我想添加错误处理module.php
以在 flash messenger 中添加所有错误消息并重定向到特定页面(在我的要求中):
public function handleError(MvcEvent $e) {
$exception = $e->getParam('exception');
$controller = $e->getTarget();
//echo $exception->getMessage(); exit;
if (!$e->getApplication()->getServiceManager()->get('AuthService')->hasIdentity()) {
$controller->flashMessenger()->addErrorMessage("Session Expired..!!");
return $e->getTarget()->plugin('redirect')->toRoute('auth', array('action' => 'login'));
}
switch ($exception->getCode()) {
case "2003" :
$controller->flashMessenger()->addErrorMessage("Unable to connect database..!!");
break;
default :
$controller->flashMessenger()->addErrorMessage($exception->getMessage());
break;
}
$e->getApplication()->getServiceManager()->get('AuthService')->clearIdentity();
return $e->getTarget()->plugin('redirect')->toRoute('auth', array('action' => 'login'));
}
但在某些错误中,它会抛出对未定义方法插件的调用,$e->getTarget()
因为在某些情况下,错误是在插件绑定之前生成的。我想要一种redirect and flash messenger plugins
无需引用任何控制器即可访问的方法。