Debugging Your Application
While using XDebug and a good IDE can be indispensable to debug your application, sometimes a quick
all you need. CodeIgniter makes that even better by bundling in the excellent Kint
debugging tool for PHP. This goes way beyond your usual tool, providing many alternate pieces of data, like formatting
timestamps into recognizable dates, showing you hexcodes as colors, display array data like a table for easy reading,
and much, much more.
By default, Kint is enabled in development and testing environments only.
It will be enabled whenever the constant
CI_DEBUG is defined and its value is truthy.
This is defined in the boot files (e.g. app/Config/Boot/development.php).
d() method dumps all of the data it knows about the contents passed as the only parameter to the screen, and
allows the script to continue executing:
This method is identical to
d(), except that it also
die() and no further code is executed this request.
This provides a backtrace to the current execution point, with Kint’s own unique spin:
For more information, see Kint’s page.
The Debug Toolbar provides at-a-glance information about the current page request, including benchmark results, queries you have run, request and response data, and more. This can all prove very useful during development to help you debug and optimize.
The Debug Toolbar is still under construction with several planned features not yet implemented.
The toolbar is enabled by default in any environment except production. It will be shown whenever the
CI_DEBUG is defined and its value is truthy. This is defined in the boot files (e.g.
app/Config/Boot/development.php) and can be modified there to determine what environment to show.
The Debug Toolbar is not displayed when your
baseURL setting (in app/Config/App.php or
app.baseURL in .env) does not match your actual URL.
The toolbar itself is displayed as an After Filter. You can stop it from ever
running by removing it from the
$globals property of app/Config/Filters.php.
Choosing What to Show
CodeIgniter ships with several Collectors that, as the name implies, collect data to display on the toolbar. You can easily make your own to customize the toolbar. To determine which collectors are shown, again head over to the app/Config/Toolbar.php configuration file:
class Toolbar extends BaseConfig
public $collectors = [
Comment out any collectors that you do not want to show. Add custom Collectors here by providing the fully-qualified class name. The exact collectors that appear here will affect which tabs are shown, as well as what information is shown on the Timeline.
Some tabs, like Database and Logs, will only display when they have content to show. Otherwise, they are removed to help out on smaller displays.
The Collectors that ship with CodeIgniter are:
Timers collects all of the benchmark data, both by the system and by your application.
Database Displays a list of queries that all database connections have performed, and their execution time.
Logs Any information that was logged will be displayed here. In long-running systems, or systems with many items being logged, this can cause memory issues and should be disabled.
Views Displays render time for views on the timeline, and shows any data passed to the views on a separate tab.
Cache Will display information about cache hits and misses, and execution times.
Files displays a list of all files that have been loaded during this request.
Routes displays information about the current route and all routes defined in the system.
Events displays a list of all events that have been loaded during this request.
In order for the Profiler to compile and display your benchmark data you must name your mark points using specific syntax.
Please read the information on setting Benchmark points in the Benchmark Library page.
Creating custom collectors is a straightforward task. You create a new class, fully-namespaced so that the autoloader
can locate it, that extends
CodeIgniter\Debug\Toolbar\Collectors\BaseCollector. This provides a number of methods
that you can override, and has four required class properties that you must correctly set depending on how you want
the Collector to work
class MyCollector extends BaseCollector
protected $hasTimeline = false;
protected $hasTabContent = false;
protected $hasVarData = false;
protected $title = '';
$hasTimeline should be set to
true for any Collector that wants to display information in the toolbar’s
timeline. If this is true, you will need to implement the
formatTimelineData() method to format and return the
data for display.
$hasTabContent should be
true if the Collector wants to display its own tab with custom content. If this
is true, you will need to provide a
$title, implement the
display() method to render out tab’s contents,
and might need to implement the
getTitleDetails() method if you want to display additional information just
to the right of the tab content’s title.
$hasVarData should be
true if this Collector wants to add additional data to the
Vars tab. If this
is true, you will need to implement the
$title is displayed on open tabs.
Displaying a Toolbar Tab
To display a toolbar tab you must:
$titlewith the text displayed as both the toolbar title and the tab header.
Optionally, implement the
display() creates the HTML that is displayed within the tab itself. It does not need to worry about
the title of the tab, as that is automatically handled by the toolbar. It should return a string of HTML.
getTitleDetails() method should return a string that is displayed just to the right of the tab’s title.
it can be used to provide additional overview information. For example, the Database tab displays the total
number of queries across all connections, while the Files tab displays the total number of files.
Providing Timeline Data
To provide information to be displayed in the Timeline you must:
formatTimelineData() method must return an array of arrays formatted in a way that the timeline can use
it to sort it correctly and display the correct information. The inner arrays must include the following information:
$data = [
'name' => '', // Name displayed on the left of the timeline
'component' => '', // Name of the Component listed in the middle of timeline
'start' => 0.00, // start time, like microtime(true)
'duration' => 0.00, // duration, like mircrotime(true) - microtime(true)
To add data to the Vars tab you must:
getVarData() method should return an array containing arrays of key/value pairs to display. The name of the
outer array’s key is the name of the section on the Vars tab:
$data = [
'section 1' => [
'foo' => 'bar',
'bar' => 'baz',
'section 2' => [
'foo' => 'bar',
'bar' => 'baz',
New in version 4.4.0.
The Debug Toolbar includes a feature called Hot Reloading that allows you to make changes to your application’s code and have them automatically reloaded in the browser without having to refresh the page. This is a great time-saver during development.
To enable Hot Reloading while you are developing, you can click the button on the left side of the toolbar that looks like a refresh icon. This will enable Hot Reloading for all pages until you disable it.
Hot Reloading works by scanning the files within the app directory every second and looking for changes. If it finds any, it will send a message to the browser to reload the page. It does not scan any other directories, so if you are making changes to files outside of the app directory, you will need to manually refresh the page.
If you need to watch files outside of the app directory, or are finding it slow due to the size of your project, you can specify the directories to scan and the file extensions to scan for in the
$watchedExtensions properties of the app/Config/Toolbar.php configuration file.