Creating and Extending CodeIgniter Helpers

CodeIgniter helpers are groups of utility functions you may need anywhere in your code. CodeIgniter has more than 20 helpers. You may be familiar with the URL, email, form or file helpers. These have functions that simplify common tasks when working with URLs, emails and files.

Whenever you feel the need to add a function to a view (which is not a good practice) it may be time to extend a CodeIgniter helper or create your own helper. For example, formatting dates directly in the view can get tedious and you may be repeating lots of code. This is specially true if you need to show time in some special, non trivial way, like showing time elapsed. In this case extending or creating a helper might be the best choice.

Extending a CodeIgniter Helper

You extend a CodeIgniter helper in a similar way to controllers and models. First save a file called "MY_[name]_helper.php" to application/helpers where [name] is the name of the helper. To extend the date helper it would be "MY_date_helper.php". There is no need to extend any class here as helpers are just a bunch of functions. Just open php tags and start writing your functions to this file. Remember not to add a closing php tag as it may cause trouble with headers. It's as simple as:

function my_first_function($params) {
    //Your code here

function my_second_function($params) {
    //Your code here

You load an extended helper the same way you load a normal helper. To load the date helper from a controller (which is the best practice) just use $this->load->helper('date_helper');. This makes your new functions available globally. Remember this means you access them directly (not with $this->date_helper->my_function()).

Creating a CodeIgniter Helper

To create a new CodeIgniter helper copy/paste the following code into a file called [name]_helper.php in application/helpers where [name] is the name of the helper:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('my_first_function'))
    function my_first_function($params)
        //Your code here

if (!function_exists('my_second_function'))
    function my_second_function($params)
        //Your code here

This codes checks if the file is being accesed directly and exits in such case. Then for each function it checks if there is a function with the same name. If there is one, it is not redefined.

After saving your helper file, you can load it in the same way you load a native helper: $this->load->helper("my_helper");. This makes your functions available globally. Now you can access your functions directly from a controller or view: $foo = my_first_function($params).

Writing helper functions

There is not much to be said on how to write helper functions. However, as helpers are procedural code, there are a few "complications" in case you need to load CodeIgniter resources.

If extending a helper, from your new functions you may call functions of the helper you are extending. In the example given above, you may call the function timespan, a "native" date_helper function.

From an extended or custom helper you may load any function of previously loaded helpers. The problem here is that you cannot count on any helper being loaded previously. You can just load the required helpers each time before using your extended helper. However, it may be preferable to load the required helpers from your new functions. To do it you can't go the usual way as there is no $this in procedural code. The easiest way is to get the $CI global object and then load the helper/library using $CI insted of $this. For example, to load the file helper and email library to send a file's content by email:

$CI =& get_instance();
$content = read_file('path');

$CI->email->subject('Sending an email from a helper');


Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>