1、創建errorhandler,繼承這個yiibaseerrorhandler抽象類,然后定義這個父類中的抽象方法
<?php namespace commoncomponentexception; /** * User: szliugx@gmail.com * Date: 2016/9/20 * Time: 14:24 */ use yii; use yiibaseErrorHandler as BaseErrorHandler; use commoncomponentearlywarningEarlyWarning; 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); ????????} ????} }
推薦教程:yii框架
2、創建視圖文件 : @app/views/errorHandler/error.php
<?php /** * User: szliugx@gmail.com * Date: 2016/9/20 * Time: 15:23 */ ?>nbsp;html> ????<meta> ????<meta> ????<meta> ????<meta> ????<meta> ????<meta> ????<meta> ????<meta> ????<title> <?php if(!empty($exception->getCode())&&($exception->getCode()?==?8)){echo?"出錯啦";}else{?echo?$exception->getMessage();}?></title> ????<link><div> ????<div></div> ????<div> ????????<p><?php if(!empty($exception->getCode())&&($exception->getCode()?==?8)){echo?"出錯啦";}else{?echo?$exception->getMessage();}?></p> ????</div> </div>
3、修改應用的配置文件:@app/config/main.php
'errorHandler'?=>?[ ????????????//'errorAction'?=>?'site/error', ????????????'class'?=>?'commoncomponentexceptionErrorHandler', ????????],
效果如下:
500錯誤頁:
404錯誤頁:
更多編程相關內容,請關注php中文網yii框架欄目!
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END