Create CodeIgniter Custom Helper

Posted On:

Posted In:

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 own 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 the 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 know how to create a custom CodeIgniter helper function. If you want more such tutorials, visit our blog 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 at +91 95 37 84 38 39

Latest Posts

Create CodeIgniter Custom Helper

Four Easy Steps to Migrate from Drupal to WordPress

May 15, 2024

While talking about CMSs, Drupal is considered by many people. It is scalable, and secure but its technicality is a…

Create CodeIgniter Custom Helper

The Ultimate Checklist for Weebly to WooCommerce Migration

April 24, 2024

Weebly’s intuitive interface has empowered countless entrepreneurs to launch their online stores with ease. However, as your business flourishes and…

Leave a Reply

Your email address will not be published. Required fields are marked *