旗下导航:搜·么
当前位置:网站首页 > PHP框架 > YII > 正文

yii2如何自定义500错误_yii框架教程,yii2,500

作者:搜搜PHP网发布时间:2020-02-21分类:YII浏览:82


导读:yii1与yii2的区别_yii框架教程yii1与yii2的区别:控制器调用区别,如yii1:【index.php?r=userGroup/index&page=1】,yii2:【...

yii1与yii2的区别_yii框架教程

yii1与yii2的区别:控制器调用区别,如yii1:【index.php?r=userGroup/index&page=1】,yii2:【index.php?r=user-group/index&page=1】。

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',
        ],

效果如下:

yii如何执行php脚本_yii框架教程

yii执行php脚本的方法:1、【yiic.bat Test Index】;2、【yiic Test Index】;3、生成脚本【.bat】文件,定时执行。

500错误页:

404错误页:

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

以上就是yii2如何自定义500错误的详细内容,更多请关注ki4网其它相关文章!

yii1.1框架如何安装_yii框架教程

yii1.1框架安装方法:1、从yii中文社区下载yii1.1压缩包,并解压到www根目录;2、运行命令【yiic webapp ../../testyii】;3、访问www/testyii,检查是否安装成功。

标签:yii2500