Codeigniter helper functions that help in Increasing Productivity

Posted By : Babbandeep Singh | 10-Dec-2017

Let's start with what is helper functions, as the title suggests, help you with tasks. All helper file is simply a set of functions of a particular kind. Codeigniter helper functions are designed to speed up and make your development easier. You can use these helper functions over and over, which help with speed and clean coding process. So that repetitive jobs can be reduced.

 

Currently, in CodeIgniter, there are 21 Helpers as following:

 

1) Array Helper
2) CAPTCHA Helper
3) Cookie Helper
4) Date Helper
5) Directory Helper
6) Download Helper
7) Email Helper
8) File Helper
9) Form Helper
10) HTML Helper
11) Inflector Helper
12) Language Helper
13) Number Helper
14) Path Helper
15) Security Helper
16) Smiley Helper
17) String Helper
18) Text Helper
19) Typography Helper
20) URL Helper
21) XML Helper

 

Loading a Helper

 

There are two ways of loading helper file.
1) If you need certain helper throughout your application, then you should just add those to your helpers autoload:

 

Like you want to load URL Helper and Form Helper, for that you have to simply change a line in the autoload.php file(/application/config/autoload.php). There you have to search for
$autoload['helper'] = array();
and change it to
$autoload['helper'] = array('
url', 'form');

 

You can load more helpers as your need, by simply writing the helper name in lowercase without using helper text, like for File Helper you can simply add 'file' as another value inside that autoload helper array.

 

 

2) What if you want to use the helper for particular controller method (or also in your View files, although that’s not a good practice). 

 

For using it in particular area, you can load a single helper as below:

 

$this->load->helper('url');

 

or multiple like this

 

$this->load->helper(array('url','language'));

 

A helper can be loaded anywhere inside your controller method or in your View files, as long as you place it before you apply it. You can load your helpers in your controller constructor so that they become accessible automatically in all function.

 

class Controller_name extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url','language'));
    }

 

or you can load a helper in a particular function that requires it. 

 

After loading the Helper File holding the function you intend to use, you can simply call it the same way you call PHP functions.
For example, to add base URL link in one of your view files using the base_url() function which is available through URL Helper, you have to write this code:

 

<?php echo base_url("blog/ci/helper"); ?>

which will result in: http://example.com/blog/ci/helper

About Author

Author Image
Babbandeep Singh

Babbandeep has experience in web development focusing on HTML, CSS, Bootstrap, JavaScript, Jquery, WordPress, Codeigniter, Magento, PHP, and MySQL. In his free time, he likes to listen music.

Request for Proposal

Name is required

Comment is required

Sending message..