FirePHP with CodeIgniter

Recently I have add a new entry to my company wiki about using FirePHP with CodeIgniter framework. I was very excited to find out that FirePHP Core Library now available in PHP4 and by referencing to an old entry Fire Ignition, I quickly create a CI wrapper that allows developer to embed FirePHP codes in their existing CI projects.

Although this page provide detail coverage on FirePHP, it mainly focus on integrating with Code Igniter, PHP4 MVC framework used for Central2. All setup and examples usage are also in PHP4.

FirePHP is an addon for FireBug, which is an extension for Firefox browser. It enables you to log to your Firebug Console using a simple PHP method call. All data is sent via response headers and will not interfere with the content on your page.

FirePHP is ideally suited for AJAX development where clean JSON and XML responses are required.

For a long time, debugging in PHP is a very cumbersome task and sometime inefficient. FirePHP provides Visual Studio like ‘trace’ and ‘watch’, which is not comprehensive but a big steps for PHP development lifecycle.

One major benefit of FirePHP is it is possible to debug a live website/web application. Specific PHP codes embedded in PHP pages that will only get triggered when client is running with FirePHP plugin installed, which will send debug messages to client console window.

Following statements are yet to be tested and verified:

  • Direct impact on performance in relation of amount of debugging message output in 1 instance.
  • All users with FirePHP installed will be able to see those debug messages.

Below is the setup environment of tested in:

  • Download FirePHP Core Library and find FirePHP.class.php4 file
  • Add a new setting in configuration file (config.php)
$config['fire_php_enabled'] = true;
  • Create a new folder in Code Igniter Library folder: firephp-[VERSION_NUMBER] and have following files in this folder
    • firephp.php : rename of FirePHP.class.php4
    • firephp_fake.php : FirePHP_FAKE class file with all attributes and properties but no implementation
    • fireigniter.php : a communicator class used by controller. This class help controller determine to load firephp.php or firephp_fake.php depends on configuration setting.
class fireigniter
{
    /**
    * Constructor
    */
    function fireigniter()
    {
        //-- Load CodeIgniter Resources
        $CI =& get_instance();

        //-- Check Configuration
        if ($CI->config->item('fire_php_enabled') == true)
        {
            //-- Load FirePHP
            $CI->load->library('firephp-0.3.1/firephp');
        }
        else
        {
            //-- Load Fake FirePHP, an empty class and name instance 'firephp' instead of 'firephp_fake'
            $CI->load->library('firephp-0.3.1/firephp_fake', 'firephp');
        }
    }
}//END class

firephp-ss-01

You are almost done. Now you need to be sure that PHP’s output buffering setting is turned on. I use WAMP package so turning this option on means clicking on WAMP icon in tray area, then selecting PHP > PHP Settings > Output buffering
  • or use inline command
ob_start();
  • Ensure Firebug ‘Net’ and ‘Console’ panels are enabled.

firephp-ss-02

Priority Logging Messages

These logging methods follow the four Firebug logging priorities. Add an optional label as a second argument to any of these methods.

$this->firephp->log($message, $label); //.. or
$this->firephp->fb($message, $label, FirePHP_LOG);
$this->firephp->info($message, $label); //.. or
$this->firephp->fb($message, $label, FirePHP_INFO);
$this->firephp->warn($message, $label); //.. or
$this->firephp->fb($message, $label, FirePHP_WARN);
$this->firephp->error($message, $label); //.. or
$this->firephp->fb($message, $label, FirePHP_ERROR);

Dumping Variable

$this->firephp->fb($variable, $key);

firephp-ss-03

Table

You can log tables of information. Firebug will display the Table Label and allow the user to toggle the display of the table. The first row of the table is automatically used as the heading and the number of columns is dynamically determined.

$table   = array();
$table[] = array('Col 1 Heading','Col 2 Heading');
$table[] = array('Row 1 Col 1','Row 1 Col 2');
$table[] = array('Row 2 Col 1','Row 2 Col 2');
$table[] = array('Row 3 Col 1','Row 3 Col 2');

$this->firephp->table($label, $table); //.. or
$this->firephp->fb($table, $label, FirePHP_TABLE);

firephp-ss-04

Following APIs has been tested and unable to perform as its documented.

Traces

$this->firephp->trace($message, $label); //.. or
$this->firephp->fb($message, $label, FirePHP_TRACE);

Dump

$this->firephp->dump($key, $variable); //.. or
$this->firephp->fb($variable, $key, FirePHP_DUMP);

class welcome extends Controller
{
	function Welcome()
	{
		parent::Controller();
        $this->load->library('firephp-0.3.1/fireigniter');
	}

	function index()
	{
        $this->firephp->fb("entering 'index' function", FirePHP_INFO);

        $var = $this->config;
        $this->firephp->fb($var, 'Current Configuration', FirePHP_INFO);

        $number = rand();
        $this->firephp->fb($number, 'Random Generated Number', FirePHP_INFO);
        echo 'Random Number is: '.$number;

        if($number % 2 == 0)
        {
            $this->firephp->fb($number, '$number before double_it()', FirePHP_INFO);
            $number = $this->double_it($number);
            $this->firephp->fb($number, '$number after double_it()', FirePHP_INFO);
        }
        else
        {
            $this->firephp->warn("Odd random number detected.");
        }

        if($number > 20000)
        {
            $this->firephp->error("Number is greater than 20000. Function terminated.");
            $this->firephp->fb($number, 'Number', FirePHP_ERROR);
            return;
        }

        echo '<br>';
        echo 'Output Number is: '.$number;
        $this->firephp->info("reaching to the end of 'index' function");
        return $number;
	}

    function double_it($n)
    {
        $output = $n * 2;
        $message = "$n x 2 = $output";
        $this->firephp->fb($message, FirePHP_LOG);
		return $output;
    }
}

Route 1: Random Number is Even and result is smaller than 20,000

firephp-ss-eg-even-smaller

Route 2: Random Number is Even and result is bigger than 20,000

firephp-ss-eg-even-smaller1

Route 3: Random Number is Odd and result is smaller than 20,000

firephp-ss-eg-odd-smaller

Route 4: Random Number is Odd and result is bigger than 20,000

firephp-ss-eg-odd-bigger

FirePHP 0.3.1 Library Packpage for CodeIgniter

firephp-0.3.1.zip

  • http://www.christophdorn.com/ Christoph Dorn

    Thanks for the detailed post!

  • http://www.travislin.com Travis

    @Christoph Dorn
    Glad that it helped you!

  • Maria

    Ouch… the link is not working. Would you send me the files or the working url by email? Please, please,please… :)

  • http://www.travislin.com Travis

    Hi Maria,

    Thanks for the feedback! This post was published over a year ago and it may now be little relevancy to current CodeIgniter, Firebug or FirePHP… but at least I will make sure the resources are accessible for all :D

  • Anonymous

    UGG Bailey Button Triplet can be worn all year as sheepskin has a huge impact on the wick moisture not only Timberland Shoes cold but also. The fluffy fibers also allow air to circulate in the trunk, keep your feet dry and comfortable at all times. However, these shoes are not designed to be waterproof, so do not try to use it in rainy weather.

    This is my family pet means of UGG Boot–Bailey Button 1873. I information two other pairs of tiny ugg boots, and like one other people they are warm, comfortable, and fashionable. They are evening simpler to acquire on contemplating that within of the slit cutting the Women’s Bailey Button Triplet part and even more versatile as well contemplating that UGG Australia Bailey Button you can fold the crown down. They are therefore selfsame wonderful looking–even ameliorate compared to picture–and exit substantially with anything, evening wonderful outfits. I wore them over the holidays using a matching bag plus they went with almost everything from pajamas through the morning to dressy clothes.

  • Playaz Uk

    Does this still work with the last version of CodeIgniter?