2012-10-03 48 views
1

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

回答

0

是你Suporte\Controller\logoff也延长了大师。如果是,那么它将导致无限循环indexController>masterController(!loggedin)>logoutController>masterController(!loggedin)>logoutController>..

0

我以前是怎么说“IT是巫术”的! 当我今天醒来时,打开我的电脑,测试控制器和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); 

}

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(); 
     } 
} 

我明白了什么: 我认为“setEventManager”是叠加的,所以将它设置为监听“dispatch”事件并调用我的verificaAuth函数重定向(如果未通过身份验证)注销或允许它进行身份验证。 希望这是有用的... 谢谢大家!