Codeigniter: Helpers, Plugins and Libraries
Having 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:
- Put the
GoogleGraph.phpfile intoapplication/helpersfolder. - 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.
- Take your
GoogleGraph.phpfile and put it into either thesystem/pluginsfolder or your ownapplication/pluginsfolder (if you don’t have the folder, just create it) - Rename the
GoogleGraph.phpfile asGoogleGraph_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.
- Take your
GoogleGraph.phpfile and put it into yourapplication/librariesfolder. - 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:
- 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.
- 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
- 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!
In: PHP Tutorials · Tagged with: codeigniter, helpers, libraries, plugins

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?
Permalink
nice writeup!
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
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.
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?
Permalink
[...] original post here: Codeigniter: Helpers, Plugins and Libraries | Ask About PHP If you enjoyed this article please consider sharing [...]
Permalink
Anyome knows a helper for interactive google charts ???????
Thanks
Tranga
Permalink
excellent post
thanks for the info, very useful
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.”
Permalink
Very good article!!! The comment written by dlogan was really interesting and helpful. Thanks.
Permalink
[...] Helpers, Plugins & Libraries [...]
Permalink
[...] Helpers, Plugins & Libraries [...]
Permalink
I definitely agree about having a scheme to organize the code. So, I’ll adopt yours. Thanks!
Permalink
[...] Helpers, Plugins & Libraries [...]