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 is just like loading anything else in CodeIgniter; it takes just one line of code.
1 |
$this->load->helper('form'); |
Loading multiple helpers
You can load multiple helpers by passing an array of values to the first parameter.
1 |
$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:
1 |
<?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.