Customizing the Profiler

Derek Jones edited this page Jul 5, 2012 · 6 revisions
Clone this wiki locally

category:Help::TipsAndTricks | Category:Help::Profiler

You want to show the profiler's output only on localhost (i.e. your development machine) and not on the bottom of the page but in a JavaScript popup window.

1) Enable hooks in application/config/config.php and add the following lines anywhere underneath $config['base_url'] = ...

$config['debug'] = ($_SERVER["REMOTE_ADDR"] == "")  ? true : false;
$config['profiler_file'] = 'profiler.html';
$config['profiler_url'] = $config['base_url'].$config['profiler_file'];

2) Add the following hook to application/config/hooks.php:

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

Note that the hook has to be post_controller_constructor or post_controller! Being executed before the controller, the hook can't access the controller and therefore not enable the profiler (see next step), being executed post_system, the profiler will not be able to output anything because the page has already been displayed.

3) Create a file called profiler.php in application/hooks and add the following code:

function enable_profiler()
  $CI =& get_instance();

4) Assuming that you have a view named foot.php which is to be displayed with every page, add the following lines:

if($this->config->item('debug') && file_exists($this->config->item('profiler_file'))):
<script language="JavaScript" type="text/javascript">'<?=$this->config->item('profiler_url')?>;', 'Profiler', 'width=950, height=400, scrollbars=yes, resizable=no, top=100, left=100');

5) Now adapt the profiler library to fit the new desires: Create MY_Profiler.php in application/libraries and add the following code:

class MY_Profiler extends CI_Profiler
  function MY_Profiler()
  function run($output = '')
      $CI =& get_instance();

        $output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  &lt;title&gt;profiler results&lt;/title&gt;

        $output .= '<div style="background-color:#fff;padding:10px;"><h1 style="margin-bottom:5px;">profiler results</h1>';

        $output .= $this->_compile_benchmarks();
        $output .= $this->_compile_post();
        $output .= $this->_compile_queries();

        $output .= '</div>';

        $output .= '&lt;/body&gt;&lt;/html&gt;';

        if(file_exists($CI->config->item('profiler_file'))) unlink&#40;$CI->config->item('profiler_file'&#41;&#41;;
        file_put_contents($CI->config->item('profiler_file'), $output);

Changes summary:

  • no modifications to CI's core -> no problems with updates whatsoever
  • modified application/config/config.php
  • modified application/config/hooks.php
  • created application/hooks/profiler.php
  • added some code to a global end view (in my case foot.php)
  • overridden original profiler library with MY_Profiler (located in application/libraries/MY_Profiler.php)