search
HomePHP FrameworkYIIHow to customize 500 error in yii2
How to customize 500 error in yii2Feb 20, 2020 pm 04:15 PM
500yii2

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

<?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);
        }
    }
}

Recommended tutorial: yii Framework

2. Create the 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;,
        ],

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!

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
yii2 怎么去掉jqueryyii2 怎么去掉jqueryFeb 17, 2023 am 09:55 AM

yii2去掉jquery的方法:1、编辑AppAsset.php文件,注释掉变量$depends里的“yii\web\YiiAsset”值;2、编辑main.php文件,在字段“components”下面添加配置为“'yii\web\JqueryAsset' => ['js' => [],'sourcePath' => null,],”即可去掉jquery脚本。

精选几道CTF习题,带你学习yii2框架!精选几道CTF习题,带你学习yii2框架!Feb 23, 2022 am 10:33 AM

本篇文章带大家了解yii2框架,分享几道CTF习题,通过它们来学习yii2框架,希望对大家有所帮助。

500internal server error什么意思500internal server error什么意思Feb 21, 2023 pm 03:39 PM

500internal server error的意思是HTTP 500内部服务器错误,表示服务器遇到意外情况,导致其无法履行请求,但它无法说明具体错误或发生错误的根本原因;当发生错误时,访问的网站会显示发生错误。

怎么使用YII2框架安装Redis扩展怎么使用YII2框架安装Redis扩展May 26, 2023 pm 06:41 PM

1.需要下载yii2-redis的master分支windows版本composer下载2.解压复制到vendor/yiisoft下面3.yiisoft下面extensions.php里面增加&#39;yiisoft/yii2-redis&#39;=>array(&#39;name&#39;=>&#39;yiisoft/yii2-redis&#39;,&#39;version&#39;=>&#39;2.0.

Ethereum (ETH) Price Recovers Above $2,320, But Struggles to Gain PaceEthereum (ETH) Price Recovers Above $2,320, But Struggles to Gain PaceSep 10, 2024 pm 03:20 PM

Ethereum price started a recovery wave above the $2,250 level. ETH was able to clear the $2,280 resistance zone to move into a positive zone, but momentum was weak compared to Bitcoin.

Bitcoin (BTC) Price Analysis: BTC Initiates Significant Upward Movement, Targets $60,000 MarkBitcoin (BTC) Price Analysis: BTC Initiates Significant Upward Movement, Targets $60,000 MarkSep 12, 2024 pm 06:35 PM

Bitcoin has initiated a significant upward movement, surpassing the $57,500 resistance level and now showing promising signs of potentially reaching the $60,000 mark.

Brits urged to check at home for rare 50p coin that could be worth £2,500Brits urged to check at home for rare 50p coin that could be worth £2,500Oct 28, 2024 pm 04:20 PM

According to one expert, the 2011 piece was minted to celebrate the London Olympics in 2012

BetMGM Michigan Bonus Code MLIVEMGM: Get a $1,500 First Bet OfferBetMGM Michigan Bonus Code MLIVEMGM: Get a $1,500 First Bet OfferNov 18, 2024 am 03:36 AM

New players can claim the BetMGM welcome bonus and get up to $1,500 paid back in bonus bets by using promo code MLIVEMGM.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)