Home  >  Article  >  PHP Framework  >  Settings for yii 500 error page

Settings for yii 500 error page

藏色散人
藏色散人Original
2020-01-11 10:57:552308browse

Settings for yii 500 error page

yii 500 error page settings

yii2 custom 500 error

Since the project wants to add early warning monitoring, one of them involves 500 errors within the program. Such error levels are relatively high, so it is necessary to capture such errors and customize a view style

Recommended learning: yii framework

After reading this blog, I know how to customize my own error page: http://tech.lubanr.com/2015/12/12/ Error and exception handling mechanism of yii2-0 framework/

If we need to customize our own exception handling method, all we need to do is inherit yii\base\ErrorHandler, write a customized renderException, and finally customize it in $config Your own errorHandler

1. Create an ErrorHandler and inherit this yii\base\ErrorHandler abstract class, and then define the abstract method in this parent class

<?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 = &#39;@app/views/errorHandler/error.php&#39;;
    public function renderException($exception)
    {
        if(Yii::$app->request->getIsAjax()){
            exit( json_encode( array(&#39;code&#39; =>$exception->getCode(),&#39;msg&#39;  =>$exception->getMessage()) ));
        }else{
            //将500的代码,发送监控预警
            if(!empty($exception->getCode()) && $exception->getCode() ==8){
                $params = [];
                $params[&#39;projectName&#39;] = "oct-youban";
                $params[&#39;level&#39;] = 5;
                $params[&#39;title&#39;] = "500:".$exception->getMessage();
                $params[&#39;value&#39;] = $exception->getCode();
                $params[&#39;message&#39;] = $exception->getFile().":".$exception->getLine();
                $params[&#39;bizcode&#39;] = 8;
                $params[&#39;subcode&#39;] = 8001;
                EarlyWarning::WarninApi($params);
            }
            echo  Yii::$app->getView()->renderFile($this->errorView,[&#39;exception&#39; => $exception,],$this);
        }
    }
}

2. Create a view file : @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. Modify the application configuration file: @app/config/main.php

        &#39;errorHandler&#39; => [
            //&#39;errorAction&#39; => &#39;site/error&#39;,
            &#39;class&#39; => &#39;common\component\exception\ErrorHandler&#39;,
        ],

Modify the above three places to achieve the desired purpose. The effect is as follows:

500 error page:

Settings for yii 500 error page

404 error page:

Settings for yii 500 error page

The above is the detailed content of Settings for yii 500 error page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to load yii css fileNext article:How to load yii css file