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
Yii2-debug 在特定页面开启调试
最新推荐文章于 2024-09-10 10:00:01 发布