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

WordPress Security Shield: The Ultimate 2024 Hack Prevention Guide

March 19, 2024

As the most used content management system (CMS), WordPress security shield is crucial. Creating a robust security system for your WordPress websites helps protect sensitive financial details and user information. Therefore, this ultimate 2024 hack…

Create CodeIgniter Custom Helper

Block Editor vs Divi vs Elementor: Choosing Best WordPress Builder

March 13, 2024

A few years back website creation demanded a lot of technical knowledge and proficiency in HTML, CSS, and JavaScript. The scenario has changed, building a user-friendly website with no code is possible. Today, a variety…

Leave a Reply

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