0

所以,我是 Zend 框架的新手,我安装了ZfcUser并且我想要当用户未登录时无法访问某些路由,例如:/blog/article/add,

实际上我<?php if($this->zfcUserIdentity()) :?>用来检查用户是否已登录,但是如果尝试访问/blog/article/add,我如何将用户重定向到我的登录路径/user,所以如果有人有任何想法,我将非常感激:)

4

2 回答 2

4

我不同意所选答案,因为在您要拒绝访问的每个控制器的每个操作中执行此类操作并不实际。

有两个大模块用于此任务。第一个存在BjyAuthorize,另一个大存在ZfcRbac。请检查一下。ZfcRbac 是我最喜欢的(因为我为它编写了文档),但它需要 PHP 5.4+

于 2014-02-22T09:37:56.423 回答
0

简单方法:

在控制器中:

if (!$this->zfcUserAuthentication()->hasIdentity()) {
    return $this->redirect()->toRoute('route_to_login',array('param' => $param));
}

更多:(在 auth zfcuset 将您重定向到以前的控制器之后)

在 module.config.php

'controllers' => array(
    'invokables' => array(
        'zfcuser' => 'Application\Controller\UserController',
    ),
...
),

接下来将文件 vendor\zf-commons\zfc-user\src\ZfcUser\Controller\UserController.php 复制到模块应用程序(与 module.config.php 中的文件夹相同)

删除除 authenticateAction 之外的所有函数添加:使用 Zend\Session\Container;并更改 return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());

$redirect_session = new Container('redirect');
        $redirect = $redirect_session->redirect;
        if($redirect!='')
        {
            unset($_SESSION['redirect']);
            return $this->redirect()->toRoute($redirect, array('lang' => $lang));
        }
        return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute(),array('param' => $param));

最后添加控制器

use Zend\Session\Container;
...
if (!$this->zfcUserAuthentication()->hasIdentity()) {
            $redirect_session = new Container('redirect');
            $redirect_session->redirect = 'route_to_this_controller_action';
            return $this->redirect()->toRoute('route_to_login',array('param' => $param));
        }
于 2014-02-21T12:18:34.230 回答