We navigate our clients from where they are to where they aspire to be.

Home / Blog / Helper in CodeIgniter

Helper in CodeIgniter

Explore the power of helpers in CodeIgniter—a collection of procedural functions that simplify common tasks like URL generation, form handling, and text formatting.

Helper in CodeIgniter

📌 Introduction
In CodeIgniter, helpers are collections of procedural functions that assist with common tasks, such as URL generation, form handling, and text formatting. They are not loaded by default, so you need to load them before use. This post explores how to load and use helpers in CodeIgniter.


🔧 What Are Helpers?
Helpers in CodeIgniter are PHP files containing functions that provide shortcuts for common tasks. Unlike libraries, which are classes, helpers are simple, procedural functions that are not tied to models, views, or controllers. They are loaded when needed and can be used globally in your application. 


📥 Loading Helpers
To use a helper, you must load it first. You can load a helper in your controller or autoload it for the entire application.

  • Load a Single Helper:

 
helper('url');
  • Load Multiple Helpers:

 
helper(['form', 'text']);
  • Autoload Helpers:
    Edit the app/Config/Autoload.php file and add the helpers to the $helpers array:

 
public $helpers = ['url', 'form'];

Once loaded, the helper functions are available globally in your controllers and views. 


🛠️ Commonly Used Helpers
CodeIgniter provides several built-in helpers:

  • URL Helper: Functions like base_url(), site_url(), and current_url() assist with URL generation.

  • Form Helper: Functions like form_open(), form_input(), and form_submit() simplify form creation.

  • Text Helper: Functions like word_limiter() and character_limiter() help with text formatting.

  • Cookie Helper: Functions like get_cookie() and set_cookie() manage cookies.

  • File Helper: Functions like read_file() and write_file() handle file operations.

These helpers are stored in the system/Helpers/ directory. You can also create your own custom helpers to encapsulate reusable functions specific to your application. 


🧩 Creating Custom Helpers
To create a custom helper:

  1. Create a PHP file in the app/Helpers/ directory, e.g., app/Helpers/my_helper.php.

  2. Define your functions in this file:

     
    if (!function_exists('my_function')) { function my_function() { // Your code here } }
  3. Load the helper in your controller:

     
    helper('my');

Now, you can use my_function() anywhere in your application. 


⚙️ Extending Built-in Helpers
If you need to add functionality to an existing helper, you can extend it:

  1. Create a new file in the app/Helpers/ directory, e.g., app/Helpers/MY_url_helper.php.

  2. Define your functions in this file:

     
    if (!function_exists('my_base_url')) { function my_base_url() { return 'https://mycustomurl.com'; } }
  3. Load the helper in your controller:

     
    helper('url');

Now, you can use my_base_url() in your application. CodeIgniter will use your extended function instead of the original one. 


🏁 Conclusion
Helpers in CodeIgniter are a powerful feature that can simplify common tasks and make your code more maintainable. By understanding how to load and use both built-in and custom helpers, you can streamline your development process and enhance the functionality of your application.

Contact Us

1119 W Duarte Rd, Arcadia, CA 91007

Solace Infotech Pvt. Ltd, Supreme HQ,
          HQ3C+9F2, Yash Orchid Society,
          Baner, Pune, Maharashtra 411021

4th Floor, Samraat Nucleus,
           Mumbai Naka, Nashik - 422001