首頁  >  文章  >  php框架  >  yii2如何自訂500錯誤

yii2如何自訂500錯誤

王林
王林原創
2020-02-20 16:15:312413瀏覽

yii2如何自訂500錯誤

1、建立ErrorHandler,繼承這個yii\base\ErrorHandler抽象類,然後定義這個父類中的抽象方法

<?php
namespace common\component\exception;
/**
 * User: szliugx@gmail.com
 * Date: 2016/9/20
 * Time: 14:24
 */
use yii;
use yii\base\ErrorHandler as BaseErrorHandler;
use common\component\earlywarning\EarlyWarning;

class ErrorHandler extends BaseErrorHandler
{

    public $errorView = &#39;@app/views/errorHandler/error.php&#39;;
    public function renderException($exception)
    {
        if(Yii::$app->request->getIsAjax()){
            exit( json_encode( array(&#39;code&#39; =>$exception->getCode(),&#39;msg&#39;  =>$exception->getMessage()) ));
        }else{
            //将500的代码,发送监控预警
            if(!empty($exception->getCode()) && $exception->getCode() ==8){
                $params = [];
                $params[&#39;projectName&#39;] = "oct-youban";
                $params[&#39;level&#39;] = 5;
                $params[&#39;title&#39;] = "500:".$exception->getMessage();
                $params[&#39;value&#39;] = $exception->getCode();
                $params[&#39;message&#39;] = $exception->getFile().":".$exception->getLine();
                $params[&#39;bizcode&#39;] = 8;
                $params[&#39;subcode&#39;] = 8001;
                EarlyWarning::WarninApi($params);
            }
            echo  Yii::$app->getView()->renderFile($this->errorView,[&#39;exception&#39; => $exception,],$this);
        }
    }
}

推薦教學:yii框架

2、建立視圖檔:@app/views/errorHandler/error.php

<?php
/**
 * User: szliugx@gmail.com
 * Date: 2016/9/20
 * Time: 15:23
 */
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="Expires" content="-1">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Pragma" content="no-cache">
    <title><?php if(!empty($exception->getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?></title>
    <link href="/css/error.css" rel="stylesheet" 0="frontend\assets\AppAsset">
</head>
<body>
<div>
    <div class="status-icon icon-desk"></div>
    <div>
        <p><?php if(!empty($exception->getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?></p>
    </div>
</div>
</body>
</html>

3、修改套用的設定檔:@app/config/main.php

&#39;errorHandler&#39; => [
            //&#39;errorAction&#39; => &#39;site/error&#39;,
            &#39;class&#39; => &#39;common\component\exception\ErrorHandler&#39;,
        ],

效果如下:

500錯誤頁:

yii2如何自訂500錯誤

#404錯誤頁:

yii2如何自訂500錯誤

更多程式相關內容,請關注php中文網程式入門欄位!

以上是yii2如何自訂500錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn