本文介绍了Yii2,抛出NotFoundException时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的代码在我的应用程序的 beforeAction 事件上运行:
I have a simple code that is running on beforeAction event of my application:
'on beforeAction' => function ($event) {
throw new \yii\web\NotFoundHttpException('The requested page does not exist.');
},
我希望它只是显示我的应用程序的 404 页面,但它会引发以下错误:
I expect it to simply show 404 page of my application, but it throws following error:
An Error occurred while handling another error:
exception 'yii\web\NotFoundHttpException' with message 'The requested page does not exist.' in /home/files/www/ucms/config/frontend/config.php:9
Stack trace:
0 [internal function]: {closure}(Object(yii\base\ActionEvent))
1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Object(Closure), Object(yii\base\ActionEvent))
2 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(607): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.php(139): yii\base\Module->beforeAction(Object(yii\web\ErrorAction))
4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('error', Array)
5 /home/files/www/ucms/vendor/yiisoft/yii2/web/ErrorHandler.php(85): yii\base\Module->runAction('site/error')
6 /home/files/www/ucms/vendor/yiisoft/yii2/base/ErrorHandler.php(109): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
7 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
8 {main}
Previous exception:
exception 'yii\web\NotFoundHttpException' with message 'The requested page does not exist.' in /home/files/www/ucms/config/frontend/config.php:9
Stack trace:
0 [internal function]: {closure}(Object(yii\base\ActionEvent))
1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Object(Closure), Object(yii\base\ActionEvent))
2 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(607): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.php(139): yii\base\Module->beforeAction(Object(yii\base\InlineAction))
4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('', Array)
5 /home/files/www/ucms/vendor/yiisoft/yii2/web/Application.php(84): yii\base\Module->runAction('', Array)
6 /home/files/www/ucms/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
7 /home/files/www/ucms/web/index.php(17): yii\base\Application->run()
8 {main}
推荐答案
ErrorHandler.php
文件中的问题 85 行:
$result = Yii::$app->runAction($this->errorAction);
当ErrorHandler
尝试运行ErrorAction
时,NotFoundHttpException
再次触发,ErrorHandler
只显示错误信息而不渲染.
When ErrorHandler
try run ErrorAction
, NotFoundHttpException
triggered again and ErrorHandler
just show error message without render.
解决方案:
public function beforeAction($action)
{
if(!$action instanceof \yii\web\ErrorAction) {
throw new \yii\web\NotFoundHttpException('The requested page does not exist.');
}
return parent::beforeAction($action);
}
上一个答案:在生产服务器上还需要在 index.php
文件中设置正确的环境设置:
Previous Answer:On production server also need setup right environments settings in your index.php
file:
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');
这篇关于Yii2,抛出NotFoundException时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!