Codeigniter JigsawHaving used Codeigniter for a few months now, this framework is really turning out to be a joy. My last post, I talked about how to modify native CI Libraries.

With so many published PHP classes and functions, it would be a shame if we couldn’t use them in CI. Fortunately CI (like all good frameworks) provides not one but three ways to integrate 3rd code, by using Helpers, Plugins and Libraries.


Of course, these ‘hooks’ are not just for integrating 3rd party codes. They are also a great way for you to organise your own PHP codes. The CI user guide has done a great job to show how to use Helpers, Plugins and Libraries, so if you haven’t already read those sections of the user guide, I would strongly encourage you to do so.

As I found out during the course of my CI project, Helpers, Plugins and Libraries are nothing more than glorified includes. I can pretty much take any 3rd party code and integrate into my application using any of the 3 methods.

Let me demonstrate by integrating the Google Graph class (by Ryon Sherman which I wrote about in my previous post) as a Helper, a Plugin and a Library, and all achieving the same result.

As a Helper
Once you download a copy of the Google Graph class (or any 3rd party class of your choice), to use it as a helper in your application. This is what you do:

  1. Put the GoogleGraph.php file into application/helpers folder.
  2. Rename the GoogleGraph.php file as GoogleGraph_helper.php

Create a new controller, like so:

class DrawGraph extends Controller {

	function index() {

		// include the 3rd party code as a helper
		$this->load->helper('googlegraph');

		$graph = new GoogleGraph();
		// set the Graph type to 'line'
		$graph->Graph->setType('line');
		// set the Size of the chart, in this case its 300x150
		$graph->Graph->setSize(300, 150);
		// define which axis we want, which is x,y only.
		// I don't need the top (t) or right (r) axis.
		$graph->Graph->setAxis(array('x','y'));	
		// set the AxisLabels for the x axis		
		$graph->Graph->addAxisLabel(array('A', 'B', 'C', 'D', 'E', 'F'));
		// set the Axis Range for the y axis 
		$graph->Graph->setAxisRange(array(1, 0, 40));
		// define the data points as an array
		$graph->Data->addData(array(5, 10, 20, 15, 30, 40));
		// finally draw the graph.	
		$graph->printGraph();
	}

}

You should get a graph like this:

As a Plugin
You can do the same thing by incorporating the GoogleGraph class as a Plugin.

  1. Take your GoogleGraph.php file and put it into either the system/plugins folder or your own application/plugins folder (if you don’t have the folder, just create it)
  2. Rename the GoogleGraph.php file as GoogleGraph_pi.php

Now, instead of loading a helper, change the controller above by changing the load helper statement:

Change
		$this->load->helper('googlegraph');
To
		$this->load->plugin('googlegraph');

You should still be able to redraw the graph.

As a Library
The same goes for using GoogleGraph class as a Library.

  1. Take your GoogleGraph.php file and put it into your application/libraries folder.
  2. This time you don’t need to rename it.

Now, instead of loading a plugin, change the controller code as follow:

Change
		$this->load->plugin('googlegraph');
To
		$this->load->library('googlegraph');

Again, you should be able to redraw the same graph without any problems.

So, when to use what?
Since, all three methods achieve the same ends. The question then is when do you use what? Fortunately, CI has also provided that distinction in their user guide which you can go read it yourself.

For me, these are my guidelines on when to use what:

  1. Plugins – I put all 3rd party codes I’m using in my application as Plugins. I would, as best as I can, try to use classes rather than straight function calls.
  2. Helpers – Any standalone straight functions calls, which are repetitive in nature, I classify them as Helpers. For example, sorting functions, my own calculation functions, etc
  3. Libraries – I classify my own classes as ‘Libraries’. Normally, if I’m already writing a class in my application, it would then to be the core logic of the application, as such, I group them all in the Library folder.

Of course all my own or 3rd party codes, I would put them in their respective folders under the application/ folder. I would not mix them into the system/ which is reserved for CI.

Whether you may have your own scheme to organize your own code, or you follow what CI user guide defined, one thing I do know, you should have a scheme to organize your codes. Without which, as your application grows in number of functions and classes, it would become a real mess, real fast!