比如我有一个 ArticleController 继承自 BaseController ,在 BaseController中,我会判断文章的是否存在,比如前台给了一个错误的 articleID ,那么我在 BaseController 中会抛出异常
throw new NotFoundHttpException('文章不存在或状态异常');
我已经在配置文件中设置了 errorAction
'errorHandler' => [
'errorAction' => 'site/error',
],
在 YII_DEBUG = true 的情况下,应该走我自定义的错误页面吧。可是结果却没有走。
追了下代码,发现问题应该出在这。
protected function renderException($exception)
{
...
$useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException);
if ($useErrorView && $this->errorAction !== null) {
$result = Yii::$app->runAction($this->errorAction); // 在这里会直接报错
if ($result instanceof Response) {
$response = $result;
} else {
$response->data = $result;
}
}
...
}