2014-12-09 5 views
0

내가 직면하고있는 다음과 같은 문제가 있습니다.컨트롤러의 ZF2 라우팅 및 리디렉션 기능

An error occurred 
An error occurred during execution; please try again later. 
Additional information: 
Zend\Mvc\Exception\DomainException 

File: .../vendor/zendframework/zendframework/library/Zend/Mvc/Controller/Plugin/Url.php:63 

Message: 
Url plugin requires that controller event compose a router; none found 

내 컨트롤러에서 리디렉션하려고하는 동안이 문제가 발생하지 않았습니다. 내가 위의 오류를 생산하는 내 컨트롤러에서 리디렉션을 위해 다음과 같은 기능을 구현한다고 가정 해 보겠습니다. 내가 도달 할 수 site.com/admin/login/ 표기가 좋아야하므로 .. 로그인 관리자의 자식으로

public function __construct() 
    { 
     # Get user identity 
     $auth = new AuthenticationService();   
     if ($auth->hasIdentity()) { 
     $this->identity = $auth->getIdentity(); 
     } else { 
     $this->redirect()->toRoute('admin/login'); 
     } 
    } 

라우팅은 존재한다. 나는 무엇이 잘못되었는지, 그리고 어떻게이 문제를 해결할 것인지 궁금해하고있다. 심지어 그것을 찾는 것이 훌륭한 출발점이 될 것이다.

감사합니다.

답변

1

컨트롤러를 구성하는 동안 리디렉션 플러그인을 사용할 수없는 것처럼 보입니다.

Url plugin requires that controller event compose a router; none found 

이렇게 코드를 onDispatch 함수에 넣는 것이 좋습니다.

public function onDispatch(MvcEvent $e) 
{ 
    # Get user identity 
    $auth = new AuthenticationService(); 
    if ($auth->hasIdentity()) { 
     $this->identity = $auth->getIdentity(); 
    } else { 
     return $this->redirect()->toRoute('admin/login'); 
    } 
    return parent::onDispatch($e); 
} 

리디렉션을 반환하는 것을 잊지 마십시오. 동작이 계속 실행됩니다.