ZF2 抛出错误并在模块.php内重定向



如果我的Module.php内有一个检查,需要抛出错误并重定向到错误页面。

有人可以建议我如何处理这个问题吗?

$eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) use($serviceManager, $capture)
        {
   if($foo == null) {
   throw new Exception("
}
}

这应该可以解决问题,尽管它需要您在 module.config 中映射错误模板:

$response = $e->getResponse();
$response->setStatusCode(500);
$response->sendHeaders();
//and in module.config
'view_manager' => array(
    'display_exceptions'       => true,
    'exception_template'       => 'error/index',

最新更新