CodeIgniter Helpers

Helper files are a collection of functions in a particular group. For example, CodeIgniter has a URL helper, which lets you easily print your website URL or the current URL, build links, and a few other functions too. Helpers can be used within Models, Controllers, and View files.
Helpers are not written in an object-oriented format in the way that Controllers and Models are, they are simply a collection of procedural functions.
It can be in the system/helpers folder or in an application/helpers folder.

Loading a helper

Loading a helper is just like loading anything else in CodeIgniter; it takes just one line of code.


Loading multiple helpers

You can load multiple helpers by passing an array of values to the first parameter.

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

Using a helper

Because helper files are procedural files, you simply use the function in the same way as you call a standard PHP function, and not in the objective format as you would
with models.
For example, to echo your site URL using the URL helper, you would use:

<?php echo base_url(); ?>

“Extending” Helpers

Even though helpers are procedural files, you can extend them in a literal sense. To do this, create a file in the system/application/helpers/ folder, with the same name as a core CI helper with the prefix MY_. You can change this prefix, but we won’t go into that right now.
Then you simply create a function inside this file with the same name as the function that you wish to replace or add a new function.
For example, if you wanted to extend the Form Helper, you would create a file called MY_form_helper.php inside the system/application/helpers/ folder, and create any functions that you wish to add.

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.

Recommended Tutorials for you

1 comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.