搜尋
首頁php框架YIIyii2如何自訂500錯誤

yii2如何自訂500錯誤

Feb 20, 2020 pm 04:15 PM
500yii2

yii2如何自訂500錯誤

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 = &#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);
        }
    }
}

推薦教學:yii框架

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

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

效果如下:

500錯誤頁:

yii2如何自訂500錯誤

#404錯誤頁:

yii2如何自訂500錯誤

更多程式相關內容,請關注php中文網程式入門欄位!

以上是yii2如何自訂500錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
YII開發人員:常見錯誤YII開發人員:常見錯誤May 12, 2025 am 12:04 AM

theSostCommonErrorsinyiiframeWorkare“ unknownproperty”,“無效configuration”,“ classNotfound”和“ valianationerationerrors” .1。 “ Unknownerproperty” errorSoccurWhenAccessingNon-existentSistentProperties; SusePropertiesexi; Susepropertiesexi;

YII開發人員:歐洲最重用的技能YII開發人員:歐洲最重用的技能May 11, 2025 am 12:02 AM

歐洲Yii開發者需具備的關鍵技能包括:1.Yii框架精通,2.PHP熟練度,3.數據庫管理,4.前端技能,5.RESTfulAPI開發,6.版本控制系統,7.測試與調試,8.安全知識,9.敏捷方法論,10.軟技能,11.本地化與國際化,12.持續學習,這些技能使開發者在歐洲市場中脫穎而出。

Yii:社區仍然活躍嗎?Yii:社區仍然活躍嗎?May 10, 2025 am 12:03 AM

Yes,theYiicommunityisstillactiveandvibrant.1)TheofficialYiiforumremainsaresourcefordiscussionsandsupport.2)TheGitHubrepositoryshowsregularcommitsandpullrequests,indicatingongoingdevelopment.3)StackOverflowcontinuestohostYii-relatedquestionsandhigh-qu

將Laravel項目遷移到YII是否容易?將Laravel項目遷移到YII是否容易?May 09, 2025 am 12:01 AM

crigatingalaravel projectToyiiishallingButachieffable withiefleflant.1)mapoutlaravel組件likeoutes,控制器和模型。 2)Translatelaravel's sartisancancancommandeloequorentoottooyii的giiandeteverecordeba

YII開發人員的基本軟技能:溝通與協作YII開發人員的基本軟技能:溝通與協作May 08, 2025 am 12:11 AM

軟技能對Yii開發者至關重要,因為它們促進團隊溝通和協作。 1)有效溝通確保項目進展順利,如通過清晰的API文檔和定期會議。 2)協作通過Yii的工具如Gii增強團隊互動,提高開發效率。

Laravel MVC:最佳好處是什麼?Laravel MVC:最佳好處是什麼?May 07, 2025 pm 03:53 PM

laravel'smvCarchitectureOfferSenhancedCodeorganization,改善確定性,andarobustseParefofConcerns.1)ItkeepscodeOdeOrganized,makenavigationNavigationAnvigationAndTeamWorkeer.2)itcompartmentalizestHeaplication,簡化了tompertalizestHeaplication,簡化了tlubloublyingttrublyingtimpertinging和maintenance.3)itse.3)itse

yii:它仍然與現代網絡開發有關嗎?yii:它仍然與現代網絡開發有關嗎?May 01, 2025 am 12:27 AM

Yiiremainsrelevantinmodernwebdevelopmentforprojectsneedingspeedandflexibility.1)Itoffershighperformance,idealforapplicationswherespeediscritical.2)Itsflexibilityallowsfortailoredapplicationstructures.However,ithasasmallercommunityandsteeperlearningcu

YII的壽命:耐力的原因YII的壽命:耐力的原因Apr 30, 2025 am 12:22 AM

Yii框架在眾多PHP框架中依然保持強大生命力是因為其高效、簡潔和可擴展的設計理念。 1)Yii通過“約定優於配置”提高開發效率;2)基於組件的架構和強大的ORM系統Gii增強了靈活性和開發速度;3)性能優化和不斷的更新迭代確保其持續競爭力。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具