我有这个问题。在我的ZF2应用程序中,我使用byjauthorize和zfcuser管理登录系统,我有两种不同的基本布局。一个专用于登录页面,另一个专用于用户登录时的应用程序。这是module.config.php中应用程序模块中的代码:
'template_map' => array(
'layout/login_layout' => __DIR__ . '/../view/layout/login_layout.phtml',
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
现在总是在Module.php中的Application模块,我设置了这段代码:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$application = $e->getTarget();
// Change default layout if user is logged
$sm = $application->getServiceManager();
$auth = $sm->get('zfcuser_auth_service');
if ($auth->hasIdentity()) {
$eventManager->attach("dispatch", function($e) {
$controller = $e->getTarget();
$s_matchedRoute = $e->getParam('route-match')->getMatchedRouteName();
if ($s_matchedRoute === 'zfcuser/login') {
$controller->layout('layout/login_layout');
} else {
$controller->layout('layout/layout');
}
});
/*
* Check Admin ACL with BjyAuthorize:
* use RedirectionStrategy (that redirects to ZfcUser's login route by default)
*/
$strategy = new RedirectionStrategy();
$strategy->setRedirectRoute('application/error');
$e->getTarget()->getEventManager()->attach($strategy);
} else {
$eventManager->attach("dispatch", function($e) {
$controller = $e->getTarget();
$controller->layout('layout/login_layout');
});
$strategy = new RedirectionStrategy();
$strategy->setRedirectRoute('zfcuser/login');
$e->getTarget()->getEventManager()->attach($strategy);
}
/**
* Format style login page
*/
$this->layoutFormLogin($eventManager);
// Setting function to handler error
set_error_handler(array('Application\Module','handlePhpErrors'));
}
但是当$ auth-> hasIdentity为false时,RedirectionStrategy不会在zfcuser / login中重定向路由。在哪里犯错? p.s我的application.config.php是这样的:
// 3rd part modules
'DoctrineModule',
'DoctrineORMModule',
'ZendDeveloperTools',
'ZfcBase',
'ZfcUser',
'ZfcUserDoctrineORM',
'BjyAuthorize',
'DOMPDFModule',
'WebinoImageThumb',
//My Module
'Application',