1、建立ErrorHandler,繼承這個yii\base\ErrorHandler抽象類,然後定義這個父類中的抽象方法
request->getIsAjax()){ exit( json_encode( array('code' =>$exception->getCode(),'msg' =>$exception->getMessage()) )); }else{ //将500的代码,发送监控预警 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、建立視圖檔:@app/views/errorHandler/error.php
getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?> getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?>
3、修改套用的設定檔:@app/config/main.php
'errorHandler' => [ //'errorAction' => 'site/error', 'class' => 'common\component\exception\ErrorHandler', ],
效果如下:
500錯誤頁:
#404錯誤頁:
更多程式相關內容,請關注php中文網程式入門欄位!
以上是yii2如何自訂500錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!