ZF2 Event before not_found_template

ⅰ亾dé卋堺 提交于 2020-01-04 05:55:55

问题


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.

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
    );

}

Anybody of you got an idea?

Thank you very much


回答1:


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);


来源:https://stackoverflow.com/questions/15576428/zf2-event-before-not-found-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!