2013-10-01 165 views
1

有没有办法,而不是显示模板404页面,有一个事件,当它没有找到一个控制器,或模块,做一个动作,如重定向?Zend框架2未找到模板

回答

2

onBootstrap方法你Module.php您可以附加功能,当一个事件发生时执行,下面附加功能时的错误(异常)被提升到执行:

public function onBootstrap(MvcEvent $e) 
{ 
    $application = $e->getApplication(); 
    $em = $application->getEventManager(); 
    //handle the dispatch error (exception) 
    $em->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'errorHandler')); 
    //handle the view render error (exception) 
    $em->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER_ERROR, array($this, 'errorHandler')); 
} 

然后再定义该函数来处理任何你想要的错误,下面是一个例子:

public function handleError(MvcEvent $e) 
{ 
    //get the exception 
    $exception = $e->getParam('exception'); 
    //...handle the exception... maybe log it and redirect to another page, 
    //or send an email that an exception occurred... 
} 
+0

我会尝试一下,看看它是否工作 – Uffo

+0

$例外= $ E-> getParam(“例外”);在404上返回null foo.com/daldnadmlamdma; d – Uffo

+0

而且我还有另一个问题,我还需要flashMessage和重定向器,我该如何获取这些内容? – Uffo