Home > PHP Framework > YII > body text

How to customize 500 error in yii2

王林
Release: 2020-02-20 16:15:31
Original
2269 people have browsed it

How to customize 500 error in yii2

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

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);
        }
    }
}
Copy after login

Recommended tutorial: yii Framework

2. Create the view file: @app/views/errorHandler/error.php





    
    
    
    
    
    
    
    
    <?php if(!empty($exception->getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?>
    

getCode())&&($exception->getCode() == 8)){echo "出错啦";}else{ echo $exception->getMessage();}?>

Copy after login

3. Modify the application configuration file: @app/config/main.php

'errorHandler' => [
            //'errorAction' => 'site/error',
            'class' => 'common\component\exception\ErrorHandler',
        ],
Copy after login

The effect is as follows:

500 error page:

How to customize 500 error in yii2

##404 error page:

How to customize 500 error in yii2

For more programming related content, please pay attention to the

Programming Introduction column on the php Chinese website!

The above is the detailed content of How to customize 500 error in yii2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!