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 = '@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 */ ?> <!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
'errorHandler' => [ //'errorAction' => 'site/error', 'class' => 'common\component\exception\ErrorHandler', ],
效果如下:
500錯誤頁:
#404錯誤頁:
更多程式相關內容,請關注php中文網程式入門欄位!
以上是yii2如何自訂500錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

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

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

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

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

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

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

禪工作室 13.0.1
強大的PHP整合開發環境

WebStorm Mac版
好用的JavaScript開發工具

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

Dreamweaver CS6
視覺化網頁開發工具