动态切换 on/off Yii 调试工具栏
Switch on/off Yii debug toolbar dynamically
是否可以动态切换 on/off Yii2 调试工具栏,例如,如果请求 url 包含 "debug=1" 或 "debug=0"?
提供了调试工具栏
通过 Yii 2 的调试扩展 https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md
此工具栏的激活与您 app/web/index.php
中这些常量的存在有关
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
事实上这些值是恒定的,这应该意味着不能在运行时更改..
所以工具栏的 activation/deactivation 只能离线使用。
或者至少它暗示了文件 index.php 中应用程序文件入口点中这些常量的更改。你可以试试动态重写代码改一下(很危险。但应该可以)
除了scaisEdge answer, you could do it as a client-side technique. In other words, using css。但是,您应该注意解决方案只是隐藏调试工具栏内容,而不是阻止它们的生成。
调试工具栏内容呈现在具有 id
属性值 yii-debug-toolbar
的 div
中。在您的主布局中,您可以执行以下操作:
<?php $this->endBody() ?><!- after this line -->
<?php if (Yii::$app->request->get('debug') == '0'):?>
<style>
#yii-debug-toolbar{
display: none !important;
}
</style>
<?php endif; ?>
</body>
更新
要维护一种从服务器端动态 show/hide 调试工具栏的方法,而不仅仅是从我原来的答案中所认为的客户端,您将不得不使用入口脚本 web/index.php
使用名为 allowedIPs
的调试模块 class 的 属性,当 debug
URL 参数等于 [=18 时,我们将在其中设置一个不可能的 IP 号码=]:
?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
/* The solution */
$config = require(__DIR__ . '/../config/web.php');
if (isset($_GET['debug']) && $_GET['debug'] == '0'){
$config['modules']['debug']['allowedIPs'] = ['1270.05.0.1'];
}
(new yii\web\Application($config))->run();
是否可以动态切换 on/off Yii2 调试工具栏,例如,如果请求 url 包含 "debug=1" 或 "debug=0"?
提供了调试工具栏 通过 Yii 2 的调试扩展 https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md
此工具栏的激活与您 app/web/index.php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
事实上这些值是恒定的,这应该意味着不能在运行时更改..
所以工具栏的 activation/deactivation 只能离线使用。
或者至少它暗示了文件 index.php 中应用程序文件入口点中这些常量的更改。你可以试试动态重写代码改一下(很危险。但应该可以)
除了scaisEdge answer, you could do it as a client-side technique. In other words, using css。但是,您应该注意解决方案只是隐藏调试工具栏内容,而不是阻止它们的生成。
调试工具栏内容呈现在具有 id
属性值 yii-debug-toolbar
的 div
中。在您的主布局中,您可以执行以下操作:
<?php $this->endBody() ?><!- after this line -->
<?php if (Yii::$app->request->get('debug') == '0'):?>
<style>
#yii-debug-toolbar{
display: none !important;
}
</style>
<?php endif; ?>
</body>
更新
要维护一种从服务器端动态 show/hide 调试工具栏的方法,而不仅仅是从我原来的答案中所认为的客户端,您将不得不使用入口脚本 web/index.php
使用名为 allowedIPs
的调试模块 class 的 属性,当 debug
URL 参数等于 [=18 时,我们将在其中设置一个不可能的 IP 号码=]:
?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
/* The solution */
$config = require(__DIR__ . '/../config/web.php');
if (isset($_GET['debug']) && $_GET['debug'] == '0'){
$config['modules']['debug']['allowedIPs'] = ['1270.05.0.1'];
}
(new yii\web\Application($config))->run();