Yii2 在特定页面中禁用调试工具栏Debug Toolbar

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

// 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

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,直接调用

use app\libs\Tools;

……

public function actionIndex()
{
    Tools::DebugToolbarOff();

    return $this->render('index');
}

发表评论

Item added to cart.
0 items - ¥0.00