ZF2事件之前not_found_template [英] ZF2 Event before not_found_template
问题描述
zf2中是否有事件可以附加到在设置了not_found_template之前调用的sharedEventManager / eventManager?
我想在我的网站上实现一个正在建设的页面模块。一旦现有的路由被调用,一切都正常。但是当一个不存在的路由被称为标准的404错误页面,因为没有找到路由。
Is there an event in zf2 i can attach to the sharedEventManager/eventManager which is called before the not_found_template is set? I want to implement a "under construction page" Module on my website. Everything works fine if a existing route is called. But when a non existing route is called the standard 404 error page is shown, because the route wasnt found.
这是我的Module.php
Thats my Module.php
public function onBootstrap(Event $e)
{
$e->getApplication()->getEventManager()->getSharedManager()->attach(
'Zend\Mvc\Controller\AbstractActionController', 'dispatch', function ($e) {
$e->getTarget()->layout('layout/underconstruction');
}, -1000
);
}
你有什么想法吗?
非常感谢
推荐答案
这是无意义的听调度事件,因为路由找不到一个控制器来调度,而不是在视图模型上监听render事件和setTemplate,这样应该可以工作
It's pointless listening to the dispatch event, since the route can't find a controller to dispatch to, instead listen to the render event and setTemplate on the view model, something like this should work
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_RENDER, function ($e) {
$response = $e->getResponse();
if ($response->getStatusCode() == 404) {
$e->getViewModel()->setTemplate('layout/underconstruction');
}
}, -1000);
这篇关于ZF2事件之前not_found_template的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!