yii 500错误页面的设置
yii2自定义500错误
由于项目想加预警监控,有一块儿是涉及到程序内部错误的500,这样的错误级别比较高,所以就需要捕获这样的错误,顺便自定义了一把视图样式
推荐学习:yii框架
看了这篇博客,知道了如何去自定义自己错误页面 : http://tech.lubanr.com/2015/12/12/yii2-0框架的错误和异常处理机制/
如果我们需要定制自己的异常处理方式,需要做的就是继承yii\base\ErrorHandler,写一个定制的renderException,最后在$config中定制自己的errorHandler
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 = '@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的代码,发送监控预警 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); } } }
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
'errorHandler' => [ //'errorAction' => 'site/error', 'class' => 'common\component\exception\ErrorHandler', ],
修改上面三处,就能达到想要的目的,效果如下:
500错误页:
404错误页 :
Atas ialah kandungan terperinci yii 500错误页面的设置. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!