CodeIgniter Helpers

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category. There are URL Helpers, that assist in creating links, there are Form Helpers that help you create form elements, Text Helpers perform various text formatting routines, Cookie Helpers set and read cookies, File Helpers help you deal with files, etc.

Unlike most other systems in CodeIgniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.

CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.

Helpers are typically stored in your system/helpers, or application/helpers directory. CodeIgniter will look first in your application/helpers directory. If the directory does not exist or the specified helper is not located there CI will instead look in your global system/helpers folder.

The url helper

The URL Helper file contains functions that assist in working with URLs.

current_url()

Returns the full URL (including segments) of the page being currently viewed. Example:

echo current_url();

// echoes 'http://mywebsite.com/contact'

uri_string()

Returns the URI segments of any page that contains this function. For example, if your URL was this:

http://some-site.com/blog/comments/123

The function would return:

/blog/comments/123

anchor()

Creates a standard HTML anchor link based on your local site URL:

echo anchor( 'contact_controller', 'Contact us!');

If you want to link an image then (see below the img helper):

echo anchor( 'homepage_controller', img('img/homepage_logo.png'));

The third parameter can contain a list of attributes you would like added to the link. The attributes can be a simple string or an associative array.

echo anchor( 'contact_controller', 'Contact us!', array('title' => 'The best news!'));

anchor_popup()

Nearly identical to the anchor() function except that it opens the URL in a new window.

mailto()

Creates a standard HTML email link. Usage example:

echo mailto('me@my-site.com', 'Click Here to Contact Me');

See the original documentation page.

The html helper

The HTML Helper file contains functions that assist in working with HTML.

img()

Lets you create HTML <img /> tags. The first parameter contains the image source. Example:

echo img('images/picture.jpg');
// gives <img src="http://site.com/images/picture.jpg" />

Additionally, an associative array can be passed to the img() function for complete control over all attributes and values. If an alt attribute is not provided, CodeIgniter will generate an empty string.

$image_properties = array(
    'src' => 'images/picture.jpg',
    'alt' => 'Image description',
    'class' => 'image_class',
    'width' => '200',
    'height' => '200',
    'title' => 'That was quite a night'
);

img($image_properties);

See the original documentation page.

The text helper

The Text Helper file contains functions that assist in working with text.

character_limiter()

Truncates a string to the number of characters specified. It maintains the integrity of words so the character count may be slightly more or less then what you specify. Example:

$string = "Here is a nice text string consisting of eleven words.";

$string = character_limiter($string, 20);

// Returns: Here is a nice text string… 

See the original documentation page.

Create your own custom helper

Rather than copy/pasting the same lines of code in every controller, it is good practice to make this function have some separate space in helpers (but not a good practice to load helper in views). How can you do this?

In application/helpers folder create a new php file custom_helper.php.

<?php

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

if (!function_exists('custom_function'))
{

    function custom_function()
    {
        // do something here
    }

}

?>

Inside helpers, you cannnot use the $this keyword, for example the code below will return an error:

$this->load->model('My_model');

To override this, you'll need to use this code:

$CI =& get_instance();
$CI->load->model('My_model');

Learning Resources

See the original documentation page.

Post A Comment

Anti-Spam Quiz: