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中文网其他相关文章!

tobuildRobustWebapplicationswithyii,MasterTheSeskills:1)MvCarchitectureForstructuringApplications,2)ActivereCordForefifficdataBaseOperations,3)widgetsystemporreusableReusableSueuiComponents,4)验证和验证和验证,5)cachingforpermificatization cachingforpermifications

tobecomeAccessfulyiideDeveloder,Youneed:1)phpmastery,2)理解,3)yiiframework-worksprofience,4)databasemanagementskills,5)前端知识,6),6)apevelight expertise,7)测试和debugggingCapabientials,8)9)

theSostCommonErrorsinyIiframeWorkare“ Unknownproperty”,“无效configuration”,“ classNotfound”和“ valianationerrors” .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


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器