How to enable profiling in CodeIgniter
Home » BLOG » Web development » How to enable profiling in CodeIgniter

How to enable profiling in CodeIgniter

category:  CodeIgniter, Web development

When you are developing the application or website with CodeIgniter, you should enable the profiling feature. It will help you see the useful data such as Benchmarks, data from $_GET, memory usage, data from $_POST, URI string, class/method, active databases, HTTP headers, session data, config variables.

To enable it, you will add the code to your controller. I add the profiling enable in the __construct method of my class. It is important you must add “parent::__construct()” in the __construct method. Otherwise, profiling won’t work. Below is the sample code.

<?php
class Users extends CI_Controller
{

    public function __construct()
    {
        // need to inherite the parent class in order to work with enable_profiler.
        parent::__construct();

        // enable profiling if we are in the development environment.
        if (ENVIRONMENT == 'development') {
            $this->output->enable_profiler(TRUE);
        }
    }

Notice that, I add the ENVIRONMENT condition in my code. This way, I can switch the ENVIRONMENT to production and the profiling won’t load on the production. If you want to learn more about the ENVIRONMENT, you can check your index.php at your webroot. The index.php is a default index page of CodeIgniter. Open the file and look for the ERROR REPORTING section. There is useful information there.

If you are new to CodeIgniter, you can read the CodeIgniter tutorial from here.

Important note:
If you use Ajax and the Ajax calls the methods in the controller, you must disable the profiling. Otherwise, the Ajax response will include the profiling with HTML which you don’t need it.