Debugging is an inevitable and necessary part of any development cycle. While CakePHP doesn't offer any tools that directly connect with any IDE or editor, CakePHP does provide several tools to assist in debugging and exposing what is running under the hood of your application.
The debug() function is a globally available function that works similarly to the PHP function print_r(). The debug() function allows you to show the contents of a variable in a number of different ways. First, if you'd like data to be shown in an HTML-friendly way, set the second parameter to true. The function also prints out the line and file it is originating from by default.
Output from this function is only shown if the core debug variable has been set to a value greater than 0.
The debugger class was introduced with CakePHP 1.2 and offers even more options for obtaining debugging information. It has several functions which are invoked statically, and provide dumping, logging, and error handling functions.
The Debugger Class overrides PHP's default error handling,
replacing it with far more useful error reports. The Debugger's
error handling is used by default in CakePHP. As with all debugging
Configure::debug must be set to a value higher than 0.
When an error is raised, Debugger both outputs information to the page and makes an entry in the error.log file. The error report that is generated has both a stack trace and a code excerpt from where the error was raised. Click on the "Error" link to reveal the stack trace, and on the "Code" link to reveal the error-causing lines.
Using the Debugger Class
To use the debugger, first ensure that Configure::read('debug') is set to a value greater than 0.
Using Logging to debug
Logging messages is another good way to debug applications, and you can use :php:class:`CakeLog` to do logging in your application. All objects that extend :php:class:`Object` have an instance method log() which can be used to log messages:
$this->log('Got here', 'debug');
The above would write
Got here into the debug log. You can use log entries
to help debug methods that involve redirects or complicated loops. You can also
use :php:meth:`CakeLog::write()` to write log messages. This method can be called
statically anywhere in your application anywhere CakeLog has been loaded:
// In app/Config/bootstrap.php App::uses('CakeLog', 'Log'); // Anywhere in your application CakeLog::write('debug', 'Got here');
DebugKit is a plugin that provides a number of good debugging tools. It primarily provides a toolbar in the rendered HTML, that provides a plethora of information about your application and the current request. You can download DebugKit from GitHub.
If your environment supplies the Xdebug PHP extension, fatal errors will show additional Xdebug stack trace details. Details about Xdebug can be found at Xdebug .