Helper in Codeigniter

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category. Every helper file is a collection of functions aiming towards a particular role.
CodeIgniter provide different types of helper class, such as url_helper, captcha_helper, email_helper and has more than 20 system helpers. All system helpers are stored in system/helpers directory.

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/ directory.

In this blog we are discuss on both built in helper and custom helper

Helper in Codeigniter

1. how to call helper function in codeigniter

Syntax : Codeigniter Load Helper

  • $this->load->helper(‘name’);

Example : Codeigniter Load Url Helper.

  • $this->load->helper(‘url’);

Codeigniter Load Multiple Helper

  • $this->load->helper(array(‘name1′,’name2′,’name3’));

Example Codeigniter Multiple Helper

  • $this->load->helper(‘url’,’form’);

Auto-load Codeigniter Helper : first of all simple Go to The Path Like as a application/config/autoload.php and then Go to Below some source Line

  • $autoload[‘helper’] = array(‘helper1′,’helper2’);

Example Codeigniter Multiple Helper : simple Load Url, form Helper.

  • $autoload[‘helper’] = array(‘url’, ‘form’);

2. How to create custom helper in codeigniter

Lets see how we can create and use custom helper in CodeIgniter.
Create Custom Helper : In application/helpers folder create a new php file app_helper.php.

if (! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
if (! function_exists(‘demo’)) {
function demo(){
// get main CodeIgniter object
$ci = get_instance();
    // Write your logic as per requirement

NOTE: In helper functions, you can not use the $this keyword to access the CI’s object, so you have to call get_instance() function, this method will give the access to the CI’s object. Using this object you load other helper, libraries ..etc.
How to use custom helper

You can load custom helper in two ways, globally or within the controller or within the controller method. Below are two approaches

How to load custom helper globally:
Open your application/config.php file and search for the helper array and add your custom helper name to the array.

| ——————————————————————-
| Auto-load Helper Files
| ——————————————————————-
| Prototype:
| $autoload[‘helper’] = array(‘url’, ‘file’);

  • $autoload[‘helper’] = array(‘app’);

How to load custom helper within the controller:
Just like any other build-in helpers you can load you custom helper

//load custom helper

How to use custom helper

After loading the helper with any of the above methods you can use the helper function in your controller and views.

// just call the function name

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems. To know techniques and tricks in Codeigniter, kindly visit- CodeIgniter Techniques and Tricks.

Need to develop a website your business? Solace Infotech is there to help you with their Codeigniter experts. Contact us to develop an effective website using codeigniter framework that will reach your business to the success that you deserves.

Related Post