Giving your Helpers a little Help
August 12th, 2007
CodeIgniter is one of the most flexible, powerful, unobtrusive frameworks you could choose to use. It’s so useful in many ways, including pre-existing convenience libraries (I can’t live without the email library anymore), plugins (for handy little tools such as javascript calendars and CAPTCHA creation) and the appropriately named “helpers”.
In the world of CodeIgniter libraries, plugins and helpers, the libraries are Rock Stars. They get most of your attention, they do most of the heavy lifting. They date super-models, party in Europe, and return on Monday to keep your application humming along (sometimes Tuesday… you know those darned unreliable rock stars). They can be extended, overwritten, and you can create your own, wholly new libraries just for your application. Wow. No wonder super-models want to date them.
One of the most handy aspects of libraries, is that you can create a libraries folder in system/application/libraries, and leave the existing CodeIgniter libraries untouched. This makes upgrading to newer versions of CI a breeze, as you never need to worry about going in and re-implementing all the changes you made to a library. From the userguide:
As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder.
And while libraries are doing all this work, we’re left with the humble, but infinitely handy helper. Here’s what the userguide has to say about helpers.
Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.
Makes them sound like one-trick ponies; but they are not. Helpers, while not quite as flexible as libraries, are tremendously useful. They are written procedurally, instead of object oriented (as the rest of CodeIgniter is). And while they can’t be extended as libraries can, a little known fact is that they can be overwritten. Better yet, is the method in which you’d override them. Much like libraries, the trick lies in creating a folder underneath system/application for helpers. Then, when CodeIgniter loads helpers, it looks first in your application/helpers folder, and only if that folder can’t be found does it then fall back on the default system/helpers folder. So you can add your own helpers, or overwrite existing ones.
Here’s a case study. In BambooInvoice I use the date helper’s timespan() function to determine exactly how overdue an invoice might be. My problem is that the default timespan() function keeps tabs down to the second of the span of time. When tracking invoices, I don’t care how many hours, minutes and seconds have passed, only how many days and weeks (or months if you have a really bad-paying client ;)) So for use in Bamboo, I want that helpers default output changed. The solution, to remove or comment out those lines of the helper.
But the problem with modifying the system helper is that in the next upgrade, that helper gets overwritten again. Since I use BambooInvoice as part of my testing codebase for vetting new CodeIgniter code, I tend to overwrite these files a lot. This then required carefully keeping track of what helpers were modified, and introduced some other problems. A preferable approach would be to simply make a copy of system/helpers/date_helper.php in application/helpers. Now, the application helper will be loaded preferentially, and I can modify to my hearts content without ever fearing I’ll accidentally overwrite my changes.
Application based helpers are a handy feature indeed. There have been discussions about changing the procedural structure of helpers to allow for extending and over-riding, but for now, the simplicity of helper functions are preserved, and you can easily maintain your own modified versions.
How are you using CodeIgniter helpers? If you’ve modified any of the base helpers, or created your own, I’d love to hear about it here.
This entry was made on August 12th, 2007 @ 18:30 and filed into CodeIgniter, How-To.

Jon wrote on August 12th, 2007 @ 21:30
I have a helper I call the “header helper”. Sometimes, I use the same html layout view over and over again - but I don’t want to include every bit of javascript/css/script on every page.
To reduce page weight, I devised the “header helper” to write out some of these things for me.
For example, let’s say I have a bunch of CSS files that I need to load - but they are in different directories (I keep them in different places depending on if they are “library” CSS like YUI or my own CSS): I would use my header_helper like this:
controller:
<?$css = array(
"/public/css"=>array("file1.css","file2.css"),
"/public/libraries/css"=>arrray("lib1.css","lib2.css")
);
?>
In the view:
<?= show_css($css) ?>Which would output something like:
<link rel="stylesheet" type="text/css" href="public/css/file1.css" />
...file2, file3
<link rel="stylesheet" type="text/css" href="/public/libraries/css/lib1.css" />
...lib2, lib3, etc
You get the idea. The same goes for external JS files. I can also write blocks of scripts (as needed) and use them as views:
$looseScript = $this->load->view("looseScript1",$data,true);In the view:
<head>
<?= $looseScript ?>