Skip to content

Customizing the Profiler

World Wide Web Server edited this page Jul 4, 2012 · 6 revisions

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. [b]Enable hooks[/b] in application/config/config.php and add the following lines anywhere [b]underneath[/b] $config['base_url'] = ... [code] <? $config['debug'] = ($_SERVER["REMOTE_ADDR"] == "127.0.0.1") ? true : false; $config['profiler_file'] = 'profiler.html'; $config['profiler_url'] = $config['base_url'].$config['profiler_file']; ?> [/code]

  2. Add the following hook to application/config/hooks.php: [code] <? $hook['post_controller_constructor'] = array( 'class' => '', 'function' => 'enable_profiler', 'filename' => 'profiler.php', 'filepath' => 'hooks', 'params' => array() ); ?> [/code] Note that the hook has to be [b]post_controller_constructor or post_controller[/b]! 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: [code] <? function enable_profiler() { $CI =& get_instance(); $CI->output->enable_profiler($CI->config->item('debug')); } ?> [/code]

  4. Assuming that you have a view named [b]foot.php[/b] which is to be displayed with every page, add the following lines: [code] <? if($this->config->item('debug') && file_exists($this->config->item('profiler_file'))): ?> <script language="JavaScript" type="text/javascript"> window.open('<?=$this->config->item('profiler_url')?>;', 'Profiler', 'width=950, height=400, scrollbars=yes, resizable=no, top=100, left=100'); </script> <? endif; ?> [/code]

  5. Now adapt the profiler library to fit the new desires: Create MY_Profiler.php in application/libraries and add the following code: [code] <? class MY_Profiler extends CI_Profiler { function MY_Profiler() { parent::CI_Profiler(); } function run($output = '') { $CI =& get_instance(); $CI->load->library("config");

     $output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    

<html> <head> <title>profiler results</title> </head>

<body>'; $output .= '

profiler results

';
    $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);
    return;
}

} ?> [/code]

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)
Clone this wiki locally