Disable Yii2 Debugger Toolbar In Specific View

 ·  · 

Method to disable Yii2 debugger toolbar.

 

1 Completely Disable

You can disable debugger toolbar completely in your Yii2 project by removing or commenting debug module in configuration file.

1.1 Basic Template

Edit /config/web.php in your project, and comment out the debug module:

if (!YII_ENV_TEST) {
    ...

    // $config['bootstrap'][] = 'debug';
    // $config['modules']['debug'] = [
    //    'class' => 'yii\debug\Module',
    //    'allowedIPs' => ['*'],
    // ];

    ...
}

1.2 Advanced Template

For advanced project template, take frontend as an example, you should edit frontend/config/main-local.php to comment out the debug module:

if (!YII_ENV_TEST) {
    ...

    // $config['bootstrap'][] = 'debug';
    // $config['modules']['debug'] = [
    //    'class' => 'yii\debug\Module',
    //    'allowedIPs' => ['*'],
    // ];

    ...
}

 

2 Disable In Specific View Only

Add following codes to the view or layout in which you want to disable the debugger toolbar.

if (class_exists('yii\debug\Module')) {
    $this->off(\yii\web\View::EVENT_END_BODY, [\yii\debug\Module::getInstance(), 'renderToolbar']);
}

Above off method of yii\web\View will detach the renderToolbar handler from the EVENT_END_BODY event, which is triggered by $this->endBody() in your view layout template. So the result is that the debugger toolbar is turned off in that specific view.