Codeigniter: Helpers, Plugins and Libraries

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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:

1
2
3
4
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:

1
2
3
4
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!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Propeller
  • Reddit
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
Posted on December 3, 2008 at 11:31 am by Eldee · Permalink
In: PHP Tutorials · Tagged with: , , ,

11 Responses

Subscribe to comments via RSS

  1. Written by Piccolo Principe on December 4, 2008 at 12:16 am
    Permalink

    “As I found out during the course of my CI project, Helpers, Plugins and Libraries are nothing more than glorified includes.”
    In fact, the helper/plugin/library loading line can be easily replaced by a require_once. It does not require a framework integrating a one-file external library. What about a bunch of classes that comes with an autoloader?

  2. Written by codeignitee on July 15, 2009 at 9:35 pm
    Permalink

    nice writeup! :-)

  3. Written by tahsin on August 11, 2009 at 3:08 pm
    Permalink

    nice helpful post. I was searching for these difference. thnks.

    by the way, any one can helpful notes on php and zend php 5 certification on newdailyblog.blogspot.com

  4. Written by Dave Strickler on December 16, 2009 at 9:04 am
    Permalink

    Very helpful – got it working for me quickly.

    I did find a ‘feature’ in the Google API. Since it uses the URL to get data, you’re limited to about 2k characters, and thus about 400 data points. You will find varying amounts depending on the char count of your data.

    This is not a limitation for the casual user, but a major problem for larger graphs.

  5. Written by dlogan on January 29, 2010 at 3:37 am
    Permalink

    While you can put things many places within CodeIgniter, there is typically a “correct” place to put the code depending on what the code does and includes. The logic and differences between a library, helper, and plugin are outlined in the CodeIgniter user manual.

    A helper is a collection of functions that are related, but not grouped in a class. A plugin is suppose to serve a single function, and a library is for custom classes.

    One of the big thing that CodeIgniter does is provide organization and in the process a modular nature to development. While all the “rules” about where to put things can easily be broken, sticking to the layout helps insure code can be used and allows you to greater leverage the capabilities Codeigniter has to offer.

    If you like, CodeIgniter will let you do data processing within a model, import a library as a helper or a helper as a library, or directly edit and modify the CodeIgniter core instead of extending it.

    Doing so will have your project be disorganized, make upgrading CodeIgniter more difficult, and generally limit the ability you are able to leverage the CodeIgniter framework.

    At that point you have to ask yourself, why am I using CodeIgniter? Why not just download a few classes/scripts from Hotscripts and attempt to integrate them with my project?

  6. [...] original post here: Codeigniter: Helpers, Plugins and Libraries | Ask About PHP If you enjoyed this article please consider sharing [...]

  7. Written by Tranga on June 30, 2010 at 9:31 am
    Permalink

    Anyome knows a helper for interactive google charts ???????

    Thanks
    Tranga

  8. Written by Mike on July 21, 2010 at 10:53 am
    Permalink

    excellent post :)
    thanks for the info, very useful

  9. Written by seth on November 11, 2010 at 12:30 am
    Permalink

    Thanks for this post! It brings out a worthwhile discussion.

    I currently agree with dlogan:
    “A helper is a collection of functions that are related, but not grouped in a class. A plugin is suppose to serve a single function, and a library is for custom classes.”

  10. Written by gerente on June 4, 2011 at 4:33 am
    Permalink

    Very good article!!! The comment written by dlogan was really interesting and helpful. Thanks.

  11. Written by Free/Open Source CodeIgniter Scripts « shivamtailor on October 11, 2011 at 7:15 pm
    Permalink

    [...] Helpers, Plugins & Libraries [...]

Subscribe to comments via RSS

Leave a Reply