In this article, I am going to explain “What is helper and How to create own helper” in CodeIgnotor
What is Helper
In CodeIgniter there are helpers which are there to help you with different tasks. Every helper file is a collection of functions aiming towards a particular role. Some of the helpers are ‘file helpers’ which help you to deal with the file, ‘text helpers’ to perform various text formatting routines, ‘form helpers’ to create form element, ‘cookie helpers’ set and read cookies, ‘URL helpers’ which assist in creating links, etc.
Loading a Helper
A helper can be loaded in controller constructor which makes them globally available, or they can also be loaded in specific functions which need them.
1 2 3 |
$this->load->helper('file_name'); |
Loading Multiple Helpers
To load multiple helpers, specify them an array
1 2 3 4 5 |
$this->load->helper( array('helper1', 'helper2', 'helper3') ); |
Helpers are not written in Object Oriented format, instead they are simple, procedural functions, independent of each other. CodeIgniter has more than 20 system helpers. All system helpers are stored in system/helpers
directory.
We have already the users table into database, where all the user data have stored. We will create a function to fetch a particular user details in our custom helper file.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); if ( ! function_exists('get_user_details')){ function get_user_details($user_id){ //get main CodeIgniter object $ci =& get_instance(); //load databse library $ci->load->database(); //get data from database $query = $ci->db->get_where('users',array('id'=>$id)); if($query->num_rows() > 0){ $result = $query->row_array(); return $result; }else{ return false; } } } |
Now 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.
1 2 3 |
$this->load->helper('custom'); |
Now we are able to use helper in controller and view, If we pass userID in this function. It will be returned user details .
1 2 3 |
$user_details = get_user_details(1); |
I hope you might have understood it properly, keep reading our other blogs posts for more coding tricks.
Thanks!!
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.