我遵循了讨论中的指示
使用不同的布局到不同的模块zend框架2 (kta回答March 29 at 13:18)
能够在应用程序的两个不同模块中使用两种不同的布局ZF2
一切正常,但我有一个问题的错误页面,特别是404
首先加载应用程序模块(应用程序的默认框架),然后加载我的自定义模块。
在管理模块的配置文件中,我排除了引用错误模板,以便查看应用模块下面的那些。
问题是,当检测到错误时,使用my自定义模块的布局,而不是在应用程序模块中。
有人能帮我吗?module.config.php (admin module)
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'template_path_stack' => array(
'admin' => __DIR__ . '/../view',
),
'template_map' => array(
'Admin/layout' => __DIR__ . '/../view/layout/layout.phtml',
'admin/index/index' => __DIR__ . '/../view/admin/index/mail.phtml',
.....
),
),
module.config.php(应用程序)
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'Application/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
'application/index/mail' => __DIR__ . '/../view/application/index/mail.phtml',
'application/index/curriculumPartial' => __DIR__ . '/../view/application/index/curriculumPartial.phtml',
'application/index/mail_admin' => __DIR__ . '/../view/application/index/mail_admin.phtml'
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
应用程序模块public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$sm = $e->getApplication()->getServiceManager();
$eventManager->getSharedManager()->attach('*', MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'onDispatchError'), 100);
}
public function onDispatchError(MvcEvent $event){
$response = $event->getResponse();
if ($response->getStatusCode() == 404) {
$url = $event->getRouter()->assemble(array(), array('name' => 'error'));
$requestUri = $event->getRequest()->getRequestUri();
$ViewModel = $event->getViewModel();
//$response->getHeaders()->addHeaderLine('Location', "$url?url=$requestUri");
$response->setStatusCode(200);
$response->sendHeaders();
$event->stopPropagation(true);
} elseif($response->getStatusCode() == 500){
//DO SOMETHING else?
return;
}
}
管理模块public function onBootstrap($e)
{
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend'Mvc'Controller'AbstractController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, ''''));
$controller->layout($moduleNamespace . '/layout');
}, 100);
}
我认为问题来自于应用模块在模块admin之前加载的事实。实际上,由于在申请表单中找不到路由,控件被传递给了模块admin。在这一点上,设置一个新的模板,然后呈现错误页面。
我通过颠倒加载模块的顺序来解决,但想知道是否有更干净的方法来做到这一点。