您的位置:首页 > 编程语言 > PHP开发

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

2018-01-29 10:08 375 查看
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中定义开始或者关闭的工具类[/code]
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
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: