php – Yii2,抛出NotFoundException时发生错误

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Yii2,抛出NotFoundException时发生错误脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个在我的应用程序的beforeAction事件上运行的简单代码
'on beforeAction' => function ($event) {
    throw new \yii\web\NotFoundHttPException('The requested page does not exist.');
},

我希望它只显示我的应用程序的404页面,但它会抛出以下错误

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))
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}
85 line上ErrorHandler.PHP文件中的问题:
$result = Yii::$app->runAction($this->errorAction);

当ErrorHandler尝试运行ErrorAction时,NotFoundHttpException再次被触发,而ErrorHandler只显示没有渲染的错误消息.

解:

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文件中设置正确的环境设置:

defined('YII_DEBUG') or define('YII_DEBUG',false);
defined('YII_env') or define('YII_ENV','PRod');

脚本宝典总结

以上是脚本宝典为你收集整理的php – Yii2,抛出NotFoundException时发生错误全部内容,希望文章能够帮你解决php – Yii2,抛出NotFoundException时发生错误所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。