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 https://:8080/index.php?r=site/show-error。您將看到一條警告訊息。

Add actionShowError Method

如果您想向用戶顯示其請求無效,您可以丟擲 yii\web\NotFoundHttpException

步驟 4 - 修改 actionShowError() 函式。

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

步驟 5 - 在位址列中鍵入地址 https://:8080/index.php?r=site/show-error。您將看到以下 HTTP 錯誤。

Modify actionShowError Method

當 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;
?>

上述配置定義了當需要顯示不帶呼叫堆疊的錯誤時,將執行 site/error 操作。

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

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

以上程式碼定義了,當發生 error 時,將渲染 error 檢視。

步驟 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 - 訪問地址 https://:8080/index.php?r=site/show-error,您將看到自定義的錯誤檢視。

Error Page
廣告

© . All rights reserved.