在Yii2特定页面如何禁用调试工具栏Debug Toolbar详解

时间:2022-09-25 11:19:23

前言

本文主要给大家介绍了关于在Yii2特定页面禁用调试工具栏Debug Toolbar的相关内容,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍:

yii2的调试工具栏,堪称神器。只要在配置文件web.php中配置好,就能全局使用

?
1
2
3
4
5
6
7
// configuration adjustments for 'dev' environment
$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'],
];

但是有的时候,在特定页面中需要禁用调试工具栏。

新建工具类Tools.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
namespace app\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']);
  }
 }
}

在需要禁用调试工具栏的地方,如某个action,直接调用

?
1
2
3
4
5
6
7
8
9
10
use app\libs\Tools;
 
……
 
public function actionIndex()
{
 Tools::DebugToolbarOff();
 
 return $this->render('index');
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://segmentfault.com/a/1190000010527967