Zend framework2 ZF2重定向出控制器

Zend framework2 ZF2重定向出控制器,zend-framework2,Zend Framework2,首先,对不起我的英语不好。 晚安/白天/下午好,具体取决于您所在的位置 对不起,如果我问一些可以在这里搜索的东西,我搜索了,甚至找到了,但也许我不明白。 我需要在我的控制器中检查身份验证,因此,我实现了一个主控制器,并将所有真正的控制器扩展到它。在我的主控制器中,我检查了身份验证并做得很好,但当我尝试重定向未经身份验证的用户时,它崩溃了! 在网上搜索时,我意识到init、preDispatch等方法甚至不存在,只是构造方法,所以我尝试了它,但在构造中没有事件管理器,所以我停在这里。。。 这是我的

首先,对不起我的英语不好。 晚安/白天/下午好,具体取决于您所在的位置 对不起,如果我问一些可以在这里搜索的东西,我搜索了,甚至找到了,但也许我不明白。 我需要在我的控制器中检查身份验证,因此,我实现了一个主控制器,并将所有真正的控制器扩展到它。在我的主控制器中,我检查了身份验证并做得很好,但当我尝试重定向未经身份验证的用户时,它崩溃了! 在网上搜索时,我意识到init、preDispatch等方法甚至不存在,只是构造方法,所以我尝试了它,但在构造中没有事件管理器,所以我停在这里。。。 这是我的代码:

public function __construct(){
    $r = new SimpleRouteStack();
    $r->addRoute('logoff', Literal::factory(array(
                                                'route'=>'/suporte/logoff',
                                                'defaults' => array(
                                                    'action'     => 'logoff',
                                                  'controller' => 'Suporte\Controller\Index',
                                                )
                                        )
                            )
    );
    $e = new MvcEvent();
    $e->setRouter($r);
    $this->setEvent($e);
    $this->getEvent()->setResponse(new Response());
    $this->getEventManager()->attach('*',array($this,'mvcPreDispatch'),100);

public function mvcPreDispatch(){
    $uri = explode('/',substr($_SERVER['REQUEST_URI'],1));
    $uri[2] = !isset($uri[2]) ? "index" : $uri[2];
    $auth = new AuthenticationService();
    $identity = $auth->getStorage()->read();
    $acl = $identity[2];

    if (!$auth->hasIdentity()){                                     
        $this->redirect()->toRoute('suporte/logoff');
    }elseif (   !$acl->hasResource($uri[0].'/'.$uri[1])             
                                        ||                          
                !$acl->isAllowed($identity[1],                      
                                $uri[0].'/'.$uri[1],                
                                $uri[2]                             
                            ) 
            )
                $this->redirect()->toRoute('logoff');
        else{
            /* $this->layout()->id = $identity[0]->getId();
            $this->layout()->nome = $identity[0]->getNome();
            $this->layout()->cargo = $identity[0]->getCargo(); */
            echo "permitido";
            //$this->layout()->recursos = $this->acl->getResources();
        }
}

您的支持\控制器\注销是否也扩展了masterclass。如果是,则将导致无限循环indexController>masterController!loggedin>logoutController>masterController!loggedin>logoutController>。

我以前怎么说这是巫术!!! 当我今天醒来,打开我的电脑,测试控制器和BAZINGA!!!正在运行正常。。。 为什么?我不敢质疑。。。 我的代码是:

abstract class PadraoControllerSuporte extends AbstractActionController{
public function setEventManager(EventManagerInterface $events){
parent::setEventManager($events);
$controller = $this;
$events->attach('dispatch', function ($e) use ($controller) {
    if (is_callable(array($controller, 'verificaAuth'))){
        call_user_func(array($controller, 'verificaAuth'));
    }
}, 100);
}

我的理解是: 我认为setEventManager是叠加的,因此它设置为侦听分派事件并调用我的VerificAuth函数,如果未通过身份验证,它将重定向到注销或允许它通过身份验证。 希望这是有用的。。。 谢谢大家

public function verificaAuth(){
    $uri = explode('/',substr($_SERVER['REQUEST_URI'],1));
    $uri[2] = !isset($uri[2]) ? "index" : $uri[2];
    $auth = new AuthenticationService();
    $identity = $auth->getStorage()->read();
    $acl = $identity[2];
    if (!$auth->hasIdentity())                                      
        $this->redirect()->toRoute('suporte/logoff');
    elseif (    !$acl->hasResource($uri[0].'/'.$uri[1])                                 !$acl->isAllowed(   $identity[1],
                                    $uri[0].'/'.$uri[1],            
                                    $uri[2]                         
                                ) 
            )
                $this->redirect()->toRoute('logoff');
        else{
            /* $this->layout()->id = $identity[0]->getId();
            $this->layout()->nome = $identity[0]->getNome();
            $this->layout()->cargo = $identity[0]->getCargo(); */
            echo "permitido";
            //$this->layout()->recursos = $this->acl->getResources();
        }
}