The helper is used as a function library, by this we can just load a helper class in the controller or view as well. CodeIgniter provide different types of helper class, such as url_helper, captcha_helper ,email_helper etc. They all are located in system/helper.
Each helper file is simply a collection of functions in a particular category. 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.
Each helper function performs one specific task, with no dependence on other functions. CodeIgniter does not load Helper function by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.
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.
First create a helper file in your helpers directory under application folder.
common_helper.php
A helper can be loaded anywhere within your controller methods or view page. 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.
The Array Helper file contains functions that assist in working with arrays.
This helper is loaded using the following code:
$this->load->helper('array');
CodeIgniter helper file is a collection of functions, it help you to do task.
Now we will create a custom helper file and a function in this helper file. Also we will use this function in controller and views.
At first we will create custom_helper.php file in application/helpers directory. The filename always would have a _helper suffix. Now create get_user_details() function, this function takes user ID argument and return the respective user details. We will use the get_instance() function for access CodeIgniter’s native resources. get_instance() function returns the main CodeIgniter object. We have assign it into the $ci variable and it will help to using CodeIgniter database functions.
We will use get_user_details() custom function in the controller and view. You should load the helper file first for use helper functions. Where “custom” is file name of the helper name, without the .php extension and the “_helper” suffix.
//load custom helper
$this->load->helper('custom');
Now we are able to use helper function in controller and views. If we pass user ID into this function, it will be returned the respective user details.
$user_details = get_user_details(1);
How to call Controller in Helper file In Codeigniter
create a function in controller_load in helper file with name controller_load when you will load controller that time you have to pass absolute path and create object of that class file it will create instance and behave whatever you want to use that controller features.
File Name :
Step Two :
now you are ready to load controller anywhere. First need to load helper file and then load function which inside helper and pass controller and function which you want to load in your page or controller.
File Name :
Trending Tutorials