Zend Framework 2,验证

时间:2014-09-01 12:50:15

标签: php zend-framework2

我需要帮助。我有这个代码来验证用户,错误是在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");
    }

}

0 个答案:

没有答案