Helper in codeigniter

Leave a Comment
Helpers, What is Helpers as the name suggests, help you with your work. Codeigniter helpers are do same thing. CodeIgniter has more than 20 helpers. Each helper file is simply a collection of functions in a particular category. Most of codeigniter helpers you use in your project. 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 of the other framework codeigniter helpers are not in OO(Object Oriented) format. Codeigniter helpers are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.

Loading Helper in codeigniter at global level

Codeigniter not load helper file by by default. We have to load helper file name in config/autoload.php. After loading helper in atuoload.php file helper is globally available in your controller and views.
/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/

$autoload['helper'] = array('url','form');

Loading Helper in codeigniter at Controller level

Loading a helper file is quite simple using the following function:
$this->load->helper('name of your helper'); 
Where name is the file name of the helper, without the .php file extension or the "helper" part. For example, to load the URL Helper file, which is named url_helper.php, you would do this:
$this->load->helper('url');
You can load helper anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it. You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.
$this->load->helper( array('url', 'file', 'form') ); 
Using above method you can load multiple helper at one time, you can specify them in an array.

0 comments:

Post a Comment

Powered by Blogger.