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

前端之家收集整理的这篇文章主要介绍了php – Yii2,抛出NotFoundException时发生错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个在我的应用程序的beforeAction事件上运行的简单代码
  1. 'on beforeAction' => function ($event) {
  2. throw new \yii\web\NotFoundHttpException('The requested page does not exist.');
  3. },

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

  1. An Error occurred while handling another error:
  2. exception 'yii\web\NotFoundHttpException' with message 'The requested page does not exist.' in /home/files/www/ucms/config/frontend/config.PHP:9
  3. Stack trace:
  4. 0 [internal function]: {closure}(Object(yii\base\ActionEvent))
  5. 1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.PHP(541): call_user_func(Object(Closure),Object(yii\base\ActionEvent))
  6. 2 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.PHP(607): yii\base\Component->trigger('beforeAction',Object(yii\base\ActionEvent))
  7. 3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.PHP(139): yii\base\Module->beforeAction(Object(yii\web\ErrorAction))
  8. 4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.PHP(455): yii\base\Controller->runAction('error',Array)
  9. 5 /home/files/www/ucms/vendor/yiisoft/yii2/web/ErrorHandler.PHP(85): yii\base\Module->runAction('site/error')
  10. 6 /home/files/www/ucms/vendor/yiisoft/yii2/base/ErrorHandler.PHP(109): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
  11. 7 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
  12. 8 {main}
  13. PrevIoUs exception:
  14. exception 'yii\web\NotFoundHttpException' with message 'The requested page does not exist.' in /home/files/www/ucms/config/frontend/config.PHP:9
  15. Stack trace:
  16. 0 [internal function]: {closure}(Object(yii\base\ActionEvent))
  17. 1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.PHP(541): call_user_func(Object(Closure),Object(yii\base\ActionEvent))
  18. 3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.PHP(139): yii\base\Module->beforeAction(Object(yii\base\InlineAction))
  19. 4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.PHP(455): yii\base\Controller->runAction('',Array)
  20. 5 /home/files/www/ucms/vendor/yiisoft/yii2/web/Application.PHP(84): yii\base\Module->runAction('',Array)
  21. 6 /home/files/www/ucms/vendor/yiisoft/yii2/base/Application.PHP(375): yii\web\Application->handleRequest(Object(yii\web\Request))
  22. 7 /home/files/www/ucms/web/index.PHP(17): yii\base\Application->run()
  23. 8 {main}
85 line上ErrorHandler.PHP文件中的问题:
  1. $result = Yii::$app->runAction($this->errorAction);

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

解:

  1. public function beforeAction($action)
  2. {
  3. if(!$action instanceof \yii\web\ErrorAction) {
  4. throw new \yii\web\NotFoundHttpException('The requested page does not exist.');
  5. }
  6.  
  7. return parent::beforeAction($action);
  8. }

上一个答案:
在生产服务器上还需要在index.PHP文件中设置正确的环境设置:

  1. defined('YII_DEBUG') or define('YII_DEBUG',false);
  2. defined('YII_ENV') or define('YII_ENV','prod');

猜你在找的PHP相关文章