Yii-错误处理

Yii包含一个内置的错误处理程序。Yii错误处理程序执行以下操作-

  • 将所有非致命的PHP错误转换为可捕获的异常。
  • 显示具有详细调用堆栈的所有错误和异常。
  • 支持不同的错误格式。
  • 支持使用控制器操作来显示错误。

要禁用错误处理程序,应在输入脚本中将YII_ENABLE_ERROR_HANDLER常量定义为false。错误处理程序被注册为应用程序组件。

步骤1-您可以通过以下方式配置它。

return [
   'components' => [
      'errorHandler' => [
         'maxSourceLines' => 10,
      ],
   ],
];

上面的配置将要显示的源代码行数设置为10。错误处理程序将所有非致命的PHP错误转换为可捕获的异常。

第2步-向SiteController添加一个名为actionShowError()的新函数。

public function actionShowError() {
   try {
      5/0;
   } catch (ErrorException $e) {
      Yii::warning("Ooops...division by zero.");
   }
   // execution continues...
}

步骤3-转到URL http:// localhost:8080 / index.php?r = site / show-error。您将看到一条警告消息。

Yii-错误处理

如果要向用户显示他的请求无效,则可以抛出yii \ web \ NotFoundHttpException

步骤4-修改actionShowError()函数。

public function actionShowError() {
   throw new NotFoundHttpException("Something unexpected happened");
}

步骤5-在地址栏中输入地址http:// localhost:8080 / index.php?r = site / show-error。您将看到以下HTTP错误。

Yii-错误处理

当YII_DEBUG常量为true时,错误处理程序将显示带有详细调用堆栈的错误。当常量为false时,将仅显示错误消息。默认情况下,错误处理程序使用这些视图显示错误-

  • @ yii / views / errorHandler / exception.php-当应该在错误中显示调用堆栈信息时,将使用视图文件。
  • @ yii / views / errorHandler / error.php-当应该显示错误而没有调用堆栈信息时,使用视图文件。

您可以使用专用的错误操作来自定义错误显示。

步骤6-config / web.php文件中修改errorHandler应用程序组件。

<?php
   $params = require(__DIR__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'components' => [
         'request' => [
            // !!! insert a secret key in the following (if it is empty) - this
               //is required by cookie validation
            'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
         ],
         'cache' => [
            'class' => 'yii\caching\FileCache',
         ],
         'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
         ],
         'errorHandler' => [
            'errorAction' => 'site/error',
         ],
         //other components...
            'db' => require(__DIR__ . '/db.php'),
      ],
      'modules' => [
         'hello' => [
            'class' => 'app\modules\hello\Hello',
         ],
      ],
      'params' => $params,
   ];
   if (YII_ENV_DEV) {
      // configuration adjustments for 'dev' environment
      $config['bootstrap'][] = 'debug';
      $config['modules']['debug'] = [
         'class' => 'yii\debug\Module',
      ];
      $config['bootstrap'][] = 'gii';
      $config['modules']['gii'] = [
         'class' => 'yii\gii\Module',
      ];
   }
   return $config;
?>

上面的配置定义了当需要在没有调用堆栈的情况下显示错误时,将执行站点/错误操作。

步骤7-修改SiteController的actions()方法。

public function actions() {
   return [
      'error' => [
         'class' => 'yii\web\ErrorAction',
      ],
   ];
}

上面的代码定义,当发生错误时,将呈现错误视图。

步骤8-在views / site目录下创建一个名为error.php的文件。

<?php
   /* @var $this yii\web\View */
   /* @var $name string */
   /* @var $message string */
   /* @var $exception Exception */
   use yii\helpers\Html;
   $this->title = $name;
?>

<div class = "site-error">
   <h2>customized error</h2>
   <h1><?= Html::encode($this->title) ?></h1>
   
   <div class = "alert alert-danger">
      <?= nl2br(Html::encode($message)) ?>
   </div>
   
   <p>
      The above error occurred while the Web server was processing your request.
   </p>
   
   <p>
      Please contact us if you think this is a server error. Thank you.
   </p>
</div>

步骤9-转到地址http:// localhost:8080 / index.php?r = site / show-error,您将看到自定义的错误视图。

Yii-错误处理

作者:terry,如若转载,请注明出处:https://www.web176.com/yii/361.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年10月23日 下午3:23
下一篇 2020年10月23日 下午3:29

相关推荐

发表回复

登录后才能评论