您可以在捕获异常后以任何方式处理异常,如下例所示,您将在全局范围内捕获异常...:
在您的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, 'handleError'));
//handle the view render error (exception)
$em->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER_ERROR, array($this, 'handleError'));
}
然后定义函数以您想要的任何方式处理错误,以下是一个示例:
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...
}