Skip to content

Instantly share code, notes, and snippets.

@ricog
Created March 12, 2012 13:59
Show Gist options
  • Save ricog/2022105 to your computer and use it in GitHub Desktop.
Save ricog/2022105 to your computer and use it in GitHub Desktop.
Include DebugKit on-the-fly when debug >= 3

This gist shows an easy way to include DebugKit as a new debug level in your CakePHP applications. You'll first need to install the DebugKit plugin and then add the following code:

CakePHP >= 2.0

In Config/bootstrap.php

// Load DebugKit only if debug level is greater than 3
if (Configure::read('debug') >= 3) {
  CakePlugin::load("DebugKit");
}

In Controller/AppController.php

Either add beforeFilter() or include the lines below.

public function beforeFilter() {
  // Load DebugKit Toolbar only if debug level is greater than 3
  if (Configure::read('debug') >= 3) {
    $this->Toolbar = $this->Components->load('DebugKit.Toolbar');
  }
  parent::beforeFilter();
}

In View/Layouts/default.ctp

Add a condition around the sql_dump element in your layout.

<?php if (in_array(Configure::read('debug'), array(1, 2))): ?>
  <?php echo $this->element('sql_dump'); ?>
<?php endif; ?>

CakePHP 1.3

In app_controller.php

public function __construct() {
  if (Configure::read('debug') >= 3) {
    $this->components[] = 'DebugKit.Toolbar';
    Configure::write('debug', 2);
  }
  parent::__construct();
}

In your view (usually views/layouts/default.ctp)

<?php if (in_array(Configure::read('debug'), array(1, 2))): ?>
  <?php echo $this->element('sql_dump'); ?>
<?php endif; ?>
@josegonzalez
Copy link

Also, I would probably include the sql_dump element if (in_array(Configure::read('debug'), array(1, 2))) if you're going to conditionally add debug_kit at your custom debug level.

@ricog
Copy link
Author

ricog commented Mar 12, 2012

Thanks for the feedback. I forgot about the old debug=3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment