毕竟,对不起我的英语不好。 晚安/白天/下午(取决于你的位置) 对不起,如果我问一些可以在这里搜索的东西,我搜索,甚至找到它,但也许我不明白。 我需要在我的控制器中检查身份验证,因此,我实现了一个主控制器并将所有的真实控制器扩展到它。在我的主控制器中,我检查身份验证并做得很好,但是当我尝试重定向未经身份验证的用户时,它会崩溃! 在网络搜索中,我意识到“init,preDispatch等”方法甚至不存在更多,只是“构造”方法,所以我尝试了它,但在构造中没有事件管理器,所以我在这里停下来... 这是我的代码:ZF2重定向出控制器
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();
}
}