搜索
首页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错误页:

f08fcd2a753306a28c23099a55ec82a.png

404错误页:

5e0b460a58c2f66f2413b09908017c4.png

更多编程相关内容,请关注php中文网编程入门栏目!

以上是yii2如何自定义500错误的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
YII开发的关键技能:构建强大的Web应用程序YII开发的关键技能:构建强大的Web应用程序May 14, 2025 am 12:03 AM

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

成为成功的YII开发人员需要哪些技能?成为成功的YII开发人员需要哪些技能?May 13, 2025 am 12:01 AM

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

YII开发人员:常见错误YII开发人员:常见错误May 12, 2025 am 12:04 AM

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

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 英文版

SublimeText3 英文版

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

SecLists

SecLists

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

安全考试浏览器

安全考试浏览器

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

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器