您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)yii2怎么自定義500錯(cuò)誤,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
1、創(chuàng)建ErrorHandler,繼承這個(gè)yii\base\ErrorHandler抽象類,然后定義這個(gè)父類中的抽象方法
<?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 = '@app/views/errorHandler/error.php'; public function renderException($exception) { if(Yii::$app->request->getIsAjax()){ exit( json_encode( array('code' =>$exception->getCode(),'msg' =>$exception->getMessage()) )); }else{ //將500的代碼,發(fā)送監(jiān)控預(yù)警 if(!empty($exception->getCode()) && $exception->getCode() ==8){ $params = []; $params['projectName'] = "oct-youban"; $params['level'] = 5; $params['title'] = "500:".$exception->getMessage(); $params['value'] = $exception->getCode(); $params['message'] = $exception->getFile().":".$exception->getLine(); $params['bizcode'] = 8; $params['subcode'] = 8001; EarlyWarning::WarninApi($params); } echo Yii::$app->getView()->renderFile($this->errorView,['exception' => $exception,],$this); } } }
推薦教程:yii框架
2、創(chuàng)建視圖文件 : @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 "出錯(cuò)啦";}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 "出錯(cuò)啦";}else{ echo $exception->getMessage();}?></p> </div> </div> </body> </html>
3、修改應(yīng)用的配置文件:@app/config/main.php
'errorHandler' => [ //'errorAction' => 'site/error', 'class' => 'common\component\exception\ErrorHandler', ],
效果如下:
500錯(cuò)誤頁(yè):
404錯(cuò)誤頁(yè):
關(guān)于yii2怎么自定義500錯(cuò)誤就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。