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中文网其他相关文章!