Otaqui.com Blog

Disable Debug Output for Ajax in CakePHP

If you are developing a CakePHP application and you have the debug value set to anything greater than 0 in your config/core.php file, you will find that Ajax requests will also get the extra information appended to the output.

In order to circumvent this add this to your “app_controller.php” file (which sits directly inside the “app/” dir, rather than in “app/controllers”):

<?php
class AppController extends Controller {
  var $components = array('RequestHandler');
  var $helpers = array('Html','Form','Ajax');
  function beforeFilter() {
    if ( $this->RequestHandler->isAjax() ) {
      Configure::write('debug',0);
    }
  }
}
?>

Note that while this will disable the debugging output, it will also have other affects too (for the life of the Ajax Request) like extending the length of time that the “schema” is cached. This should make little or no difference, but is worth remembering.