我需要帮助。我有这个代码来验证用户,错误是在init
函数中没有执行函数mvcPreDispatch
。谢谢!
public function init(ModuleManager $moduleManager)
{
$sharedEvents = $moduleManager->getEventManager()->getSharedManager();
$sharedEvents->attach('zend\Mvc\Controller\AbstractActionController',
MvcEvent::EVENT_DISPATCH, array($this, 'mvcPreDispatch'), 100);
}
public function mvcPreDispatch($e)
{
$auth = new AuthenticationService;
$auth->setStorage(new SessionStorage("Admin"));
$controller = $e->getTarget();
$matchedRoute = $controller->getEvent()->getRouteMatch()->getMatchedRouteName();
if (!$auth->hasIdentity() and ($matchedRoute === 'admin' OR $matchedRoute === 'admin/paginator'))
{
return $controller->redirect()->toRoute("auth");
}
}