Enable Site-Wide Profiling With CodeIgniter

A very cool part of CodeIgniter is its ability to give you the ‘profiling’ information for page loads. That is, if you add:


In your controller before you load a view, CodeIgniter will give you information regarding how fast the page loaded, how many SQL queries executed, the content of each query, and the running time of each query. This is incredibly useful when you are trying to debug your application, or simply see how quickly things are loading.

There’s only one problem: To enable profiling, that line of code above must be present. What if you want to profile several pages, or even your whole web application? In that case, you have to start thinking:

  • I could put that line in the constructor of my controller, and then of of that controller’s methods will be profiled.
  • I could put that line in each method I want to profile.

These methods start to get ugly. And of course, you don’t want to comment out each profiling line when you don’t need them.

My method to tackle this problem involves using CodeIgniter’s Hooks feature to enable or disable profiling for the entire web applications based on a value in the configuration file. In the end, I can turn on profiling for my entire website via a config value by setting it to true or false. This is my method:

1. Enable hooks in your CodeIgniter Application by going to config/config.php end setting the flag to true:

$config['enable_hooks'] = TRUE;

2. Create a file in the config directory named hooks.php if it does not already exist. Inside it, place:

$hook['post_controller_constructor'][] = array(
                                'class'    => 'ProfilerEnabler',
                                'function' => 'EnableProfiler',
                                'filename' => 'hooks.classes.php',
                                'filepath' => 'hooks',
                                'params'   => array()

3. Create a folder in your application directory named hooks (If it does not already exist). Inside it, create a file named hooks.classes.php. Inside it, put:

class ProfilerEnabler
	function EnableProfiler()
		$CI = &get_instance();
		$CI->output->enable_profiler( config_item('enable_profiling') );

4. Finally, add this line to your application’s main configuration file:

$config['enable_profiling'] = false;

Setting this value to true will enable profiling across your entire website. Now, you can zip through pages, checking the load times of each without any trouble!

Note: I’ve always thought that an additional way to do this would be to extend the Controller class and enable profiling from there. But this method, I think, has the least impact on existing code.

This entry was posted in CodeIgniter, PHP Development and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • http://kylefarris.com Kyle Farris

    Excellent advice. Thanks!

  • http://twitter.com/johnbellone jb

    Nice post. I was always wondering about the Codeigniter profiler but never got around to using it.

  • Pingback: Enable Site-Wide Profiling With CodeIgniter | Coder Online

  • http://blog.drewsbox.com Andrew

    Hey thanks a ton for this post, it really helped me in the right direction while I’m beginning to learn about CodeIgniter hooks, which might just be the key to some problems in my current project!

    Thanks again, great article!

  • Pingback: Programowanie w PHP » Blog Archive » Kenny Katzgrau’s Blog: Enable Site-Wide Profiling With CodeIgniter

  • http://www.mersinuni.net mirza

    thanks it works perfectly.

  • may

    This was extremely useful.

  • G Money

    Thanks for putting this tutorial together, saved me a lot of time

  • sandeep kumar

    Thank Kenny .You saved my ass. Love you

  • MrMahi

    Great! Thx a lot!

  • Emily

    Thank you so much for your information. You’re really saved my time :) Appreciate much

  • madhu

    Great job. Thanks for your time.