forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
Customizing the Profiler
Derek Jones edited this page Jul 5, 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.
- Enable hooks in application/config/config.php and add the following lines anywhere underneath $config['base_url'] = ...
<?
$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'];
?>
- 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.
- Create a file called profiler.php in application/hooks and add the following code:
<?
function enable_profiler()
{
$CI =& get_instance();
$CI->output->enable_profiler($CI->config->item('debug'));
}
?>
- 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">
window.open('<?=$this->config->item('profiler_url')?>;', 'Profiler', 'width=950, height=400, scrollbars=yes, resizable=no, top=100, left=100');
</script>
<?
endif;
?>
- 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()
{
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 .= '<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 .= '</body></html>';
if(file_exists($CI->config->item('profiler_file'))) unlink($CI->config->item('profiler_file'));
file_put_contents($CI->config->item('profiler_file'), $output);
return;
}
}
?>
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)