Sunday, November 17, 2024
Google search engine
HomeLanguagesHelpers in Codeignitor

Helpers in Codeignitor

Helpers are the reusable code in codeignitor like libraries. The only difference is that libraries are collection of classes whereas helper is defined as individual independent set of functions. Helper functions need to be loaded before using it. We can find all the helpers in codeignitor documentation Codeignitor Helpers and that can be used depends on kind of requirement.
Create a controller users.php and then we can use below code to use helper.

Controller: users.php




<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
    public function index() {
  
        // Load form helper
        $this->load->helper('form');
    }   
}
?>


If we need to load multiple helpers, we can create an array and then we can define all the helper’s name in that array.

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

Custom Helpers: Codeignitor has already a lot of built-in helpers but if we need to create a function which is not in the helper then we can create our own custom helper and use it in the same way like inbuilt helpers. Inbuilt helpers are available in system folder but custom helper needs to be created in application/helpers folder. Create a file abc_helper.php in application/helpers folder. Below is the example of creating functionality in our custom helper.

Custom Helper: abc_helper.php




<?php 
    function test() {
      echo "Custom helper for codeignitor";
    }
?>


Custom helpers can be used just like inbuilt helpers in the controller. So in our users.php controller use the code below to check this.

Controller: users.php




<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
    public function index() {  
  
         // Load custom helper
         $this->load->helper('abc');
         test();
       }    
  }
?


Output:

Custom helper for codeignitor

Extending Helpers: If we want to add some other function in already inbuilt helpers, we can do that easily. Extending a helper means adding more function in inbuilt helpers or overriding inbuilt functions. It is also created in the same folder application/helpers just like we were doing for custom helpers. Now here we need to keep in mind that we need to add ‘MY_’ prefix when we give the name for our helper file. So create a file MY_array_helper.php in application/helpers folder and use the code below.

Extended Helper: MY_array_helper.php




<?php 
    // Add a function in array inbuilt helper
    function test() {
       echo "Extend array helper in codeignitor";
    }
?>


Once the array helper is extended, we can use its additional function ‘test’ in our controller. So in controller users.php use the code below to call ‘test’ function from array helper and then we will be able to use our own function.

Controller: users.php




<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
    public function index() {  
        $this->load->helper('array');
  
        // Call the function
        test();
    }    
 }
?>


Output:

Extend array helper in codeignitor

Till now we were extending our helpers and adding our own functionality, now if want to override a function of the inbuilt helper, we can also do that. To override a function from the inbuilt helper, we need to give the same function name which already exists in that helper. So, create a file MY_array_helper.php in application/helpers folder and create a function ‘element’ which already exists in system/helpers/array_helper.php.

Extended Helper: MY_array_helper.php




<?php 
    // Override the element function of array helper.
    function element() {
        echo "Overridden extended array helper in codeignitor";
    }
?>


Now in our controller users.php, if we will load array helper and call ‘element’ function it will give the result of our overridden function of array helper instead of ‘element’ function of system/helpers/array_helper.php. It will completely override the functionality of array helper element function and will give a new result which we have defined in ‘MY_array_helper.php’.

Controller: users.php




<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
    public function index() {  
        $this->load->helper('array');
  
        // Define array for element function
        $arr= ['abc'=>'ABC', 'xyz'=>'XYZ'];
  
        // Call element function but it won't 
        // give the result of element function from 
        // inbuilt array helper exist in system folder
        echo element('abc', $arr, 'Not Found');
    }    
}
?>


Output:

Overridden extended array helper in codeignitor

So from the above example its clear that once we override the function of the inbuilt helper array it gives us the result of our own defined ‘element’ function in MY_array_helper.php instead of older one ‘element’ function in array helper. If we will delete our file MY_array_helper.php, we will get the result of ‘element’ function which exists in system/helpers/array_helper.php
Output:

ABC

RELATED ARTICLES

Most Popular

Recent Comments