Yii2-debug 在特定页面开启调试

debug调试器,是简单的bug收集工具,在调试问题的时候非常有用。

但是当上线了之后,有两种方法来进行调试

1、制定特定的ip

在allowedIPs中加入当前网络的ip,这样就可以只有当前网络可见了,这是最常用也是最好的调试方式
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
    'class' => 'yii\debug\Module',
    // uncomment the following to add your IP if you are not connecting from localhost.
    'allowedIPs' => ['127.0.0.1', '::1'],
];

2、只允许特定页面进行显示
(1)首先页面要开启debug
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
    'class' => 'yii\debug\Module',
    // uncomment the following to add your IP if you are not connecting from localhost.
    'allowedIPs' => ['127.0.0.1', '::1'],
];
(2)在commen/libs中定义开始或者关闭的工具类
namespace common\libs;

use Yii;
class Tools
{
    public static function DebugToolbarOff(){
        if(class_exists('\yii\debug\module')){
            Yii::$app->view->off(\yii\web\View::EVENT_END_BODY,[yii\debug\Module::getInstance(),'renderToolbar']);
        }
    }
    public static function DebugToolbarOn(){
        if (class_exists('\yii\debug\module')){
            Yii::$app->view->on(yii\web\View::EVENT_END_BODY,[yii\debug\Module::getInstance(),'renderToolbar']);
        }
    }
}
(3)在所有的页面禁用debug
在继承的controller中取消
public function __construct($id, $module, $config = [])
{
    Tools::DebugToolbarOff();
    parent::__construct($config);
}
(4)在要显示的页面开启debug
public function actionAdd () {

    Tools::DebugToolbarOn();
    ...
}









查看原文:http://www.architecy.com/archives/397
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值