yii2学习笔记——自定义报错机制

来源:互联网 发布:ubuntu 文件夹root权限 编辑:程序博客网 时间:2024/05/16 02:26

yii2框架提供了很方便的开发环境和生产环境两种环境,一方面方便开发者进行调试的时候能看到详细的错误信息,另一方面在开发者把代码转移到生产环境上时防止了隐私泄露。

一.设置错误信息行数
app/config/web.php 中向$config中的components中的errorHandler中添加:

'maxSourceLines'=>5

这里写图片描述

二.设置自定义404界面
app/config/web.php 中向$config中的components中的errorHandler中添加:

'errorAction' => 'error/error'

具体可见上图,把其中的site改成error即可,然后界面需要在app/views/site/error.php中自己写,这里贴出默认的代码

<?php/* @var $this yii\web\View *//* @var $name string *//* @var $message string *//* @var $exception Exception */use yii\helpers\Html;$this->title = $name;?><div class="site-error">    <h1><?= Html::encode($this->title) ?></h1>    <div class="alert alert-danger">        <?= nl2br(Html::encode($message)) ?>    </div>    <p>        The above error occurred while the Web server was processing your request.    </p>    <p>        Please contact us if you think this is a server error. Thank you.    </p></div>

这里写图片描述

三.改变环境模式

app/web/index.php中有两行代码

defined('YII_DEBUG') or define('YII_DEBUG', true);defined('YII_ENV') or define('YII_ENV', 'dev');

当需要是工作环境模式时,设置如下

//defined('YII_DEBUG') or define('YII_DEBUG', true);//defined('YII_ENV') or define('YII_ENV', 'dev');defined('YII_ENV') or define('YII_ENV', 'prod');

如果是开发环境,设置不变

这里写图片描述

0 0