What is Helper in CodeIgniter? 

A helper is a collection of functions that helps you easily carry out a specific task. These functions are stored in the Helper file. There are in-built Helpers to help you out with various tasks such as text helper helps you with various text formatting tasks, form helper creates form elements, cookie helper sets and reads cookies, and URL helper helps in creating links.  

Helper functions are not written in object-oriented format. They are written in a simple, procedural language as independent from each other. You can use these helper files after loading them. Once loaded, it will be globally available to your controllers and views.

If you cannot find a suitable Helper to perform a specific task, you can create your custom helper class.

 

How to create a custom Helper? 

Step 1: Create Custom Helper 

Navigate to the application/helper folder and create a new app_helper.php file.

 

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’); 

 

function demo() { 

   // get main CodeIgniter object 

    $ci = get_instance(); 

   // Write your logic here 

} 

NOTE: You can’t access the CI object with $this keyword in helper functions, so you have to use the get_instance() method to access the CI object. 

 

Step 2: Load Custom Helper 

You can load a custom helper in two ways: 

  1. Globally 
  1. Within the controller or controller method 

 

Load the Custom Helper Globally 

Open your application/config.php file and add your custom helper name in the Helper array. 

$autoload[‘helper’] = array(‘app‘); 

 

Load Custom Helper Within the controller 

Loading a custom helper in a controller is the same as loading a built-in helper. 

$this->load->helper(‘app’); 

 

Step 3: Use Custom Helper 

Now your Helper function is ready to be used in controller and views. 

Just call the function name, and you are good to go. 

demo();

 

Conclusion

Hope now you got how to create a custom CodeIgniter helper functions. If you want more such tutorials, visit our blogs page. Share this post with your friends and social media followers.

If you are looking for a software firm to create your business website or mobile app, your search ends here. We build affordable and robust software solutions for businesses to digitally thrive on. Contact us to get a quote. You can mail us at [email protected] or WhatsApp us on +91 95 37 84 38 39.