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
1 min read

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 ENVIRONMENT, you can check your index.php at your web root. The index.php is a default index page of CodeIgniter. Open the file and look for ERROR REPORTING section. There is a useful information there.

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