If Only I Knew That!

Webby things that could save you time

Codeigniter 2.0 configurable profiler + some jQuery magic to show/hide

with 3 comments

The new CI 2.0 Profiler has configurable sections + 2 new additional sections (http_headers and config).

You’ll need to edit application/config/profiler.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Profiler Sections
| -------------------------------------------------------------------------
| This file lets you determine whether or not various sections of Profiler
| data are displayed when the Profiler is enabled.
| Please see the user guide for info:
|
|	http://codeigniter.com/user_guide/general/profiling.html
|
*/

$config['benchmarks'] 		= TRUE;
$config['get'] 				= TRUE;
$config['memory_usage'] 	= TRUE;
$config['post'] 			= TRUE;
$config['uri_string']		= TRUE;
$config['controller_info']	= TRUE;
$config['queries'] 			= TRUE;
$config['http_headers'] 	= TRUE;
$config['config'] 			= TRUE;

/* End of file profiler.php */
/* Location: ./application/config/profiler.php */

I previously used a modified version of MY_Profiler from dragffy.com. I modified it to add a button to the top of the page that would show or hide the profiler when needed and could also hide the profiler button totally.

The screen grabs show hidden/visible states with benchmark section visible.

Here is the code for anyone interested – credit to dragffy.com for original inspiration

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Requires jquery in head of view
 *
 */
class MY_Profiler extends CI_Profiler {

 	function __construct($config = array())
 	{
 		parent::__construct($config);
 	}

	// --------------------------------------------------------------------
	
	/**
	 * Run the Profiler
	 *
	 * @access	private
	 * @return	string
	 */	
	function run()
	{
		$output = <<<ENDJS
<script type="text/javascript" language="javascript">
// < ![CDATA[
    $(function() {
		var profiler_html = $('#codeigniter_profiler').clone();
		var profiler_bar_html = $('#profiler_bar').clone();
		
        $('#codeigniter_profiler, #profiler_bar').remove();
		
		$('body').prepend(profiler_bar_html);
		$('body').append(profiler_html);
		
		$('#codeigniter_profiler').hide();
		$('#profiler_btn').click(function(){
			$('#codeigniter_profiler').toggle();
			
			if($('#codeigniter_profiler').is(':visible')) {
				$('#profiler_btn').text('HIDE PROFILER');
			} else {
				$('#profiler_btn').text('SHOW PROFILER');
			}
			
			return false;
		});
		$('#profiler_close_btn').click(function(){
			$('#profiler_bar, #codeigniter_profiler').hide();
			return false;
		});
    });
// ]]>
</script>
ENDJS;

		$output .= "<div id='profiler_bar' style='background-color: green; width: 150px; height: 20px; padding: 2px; position: absolute; top: 0; left: 20px; text-align: center; color: #FFF; font-family: Verdana, Geneva, sans-serif; filter: alpha(opacity=30); -moz-opacity: 0.30; opacity: 0.30;'><a href='#' id='profiler_btn' style='color: #FFF; text-decoration: none;'>SHOW PROFILER</a><a href='#' id='profiler_close_btn' style='color: #FFF; text-decoration: none; margin-left:10px'>X</a></div>";

		$output .= "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
		$fields_displayed = 0;
		
		foreach ($this->_available_sections as $section)
		{
			if ($this->_compile_{$section} !== FALSE)
			{
				$func = "_compile_{$section}";
				$output .= $this->{$func}();
				$fields_displayed++;
			}
		}

		if ($fields_displayed == 0)
		{
			$output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
		}
		
		$output .= '</div>';

		return $output;
	}

}

// END MY_Profiler class

/* End of file MY_Profiler.php */
/* Location: ./application/libraries/MY_Profiler.php */

Written by daveganley

May 4th, 2010 at 1:22 pm

Posted in Codeigniter,PHP

3 Responses to 'Codeigniter 2.0 configurable profiler + some jQuery magic to show/hide'

Subscribe to comments with RSS or TrackBack to 'Codeigniter 2.0 configurable profiler + some jQuery magic to show/hide'.

  1. Hello ,

    I tried it and it’s not working steps i made

    1- Add profiler code to “application/config/profiler.php”

    2- Add MY_Profiler.php to “application/core”

    3- Load latest jquery using Google Load http://codeigniter.com/wiki/Google_Load/

    4- Add “” to my viewer

    i don’t know why it not working i’m using CI 2.0

    in firebug jquery working and loaded before rendering the view and debug div in the footer

    did i missed any thing ?

    Ahmed Abu Eldahab

    19 Oct 10 at 6:55 pm

  2. Hi
    I solved the problem

    i forgot to enable debug from the config and add the code in controller

    Regards

    Ahmed Abu Eldahab

    19 Oct 10 at 9:27 pm

  3. Glad you got it sorted, hope it helps with your CI dev

    admin

    20 Oct 10 at 8:07 am

Leave a Reply

Powered by CDN Rewrites