Advertisement

Google Ad Slot: content-top

CodeIgniter Helpers


What is a Helper?

In CodeIgniter, a Helper is simply a collection of PHP functions (not classes) that make your work easier by providing shortcuts for common tasks.

  • They are not tied to Models, Views, or Controllers.
  • They don’t maintain state — they are just simple utility functions.
  • You load them when you need them, and then you can call their functions directly.

👉 Think of a helper like a toolbox: whenever you need a small tool (like a URL formatter, string handler, or date formatter), you just open the toolbox and use it.


Where Helpers Live in CodeIgniter

Helpers are stored in:

system/Helpers/    (Core helpers provided by CodeIgniter itself)
app/Helpers/      (Custom helpers you create for your own project)


Loading a Helper

You can load a helper in two ways:

1. Load in a Controller

helper('url');

After this, you can immediately use URL functions like base_url().

2. Auto-load in Config

Edit this file:

app/Config/Autoload.php

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

Now, the url and form helpers will load automatically for all controllers/views.

Commonly Used CodeIgniter Helpers

Helper Name

Purpose 

Example Function

url

Work with URLs

base_url('path')

form

Create form elements

form_open('route')

text

Format and work with text

word_limiter($string, 5)

html

Generate HTML elements

img('path/to/image.jpg')

date

Format and manage dates

now()

number

Format numbers

number_to_currency(1500, 'USD')

Example: Using URL Helper


public function index()
{
helper('url');

echo base_url('products/view/10');
}

👉 This is useful when generating links dynamically.

Example: Using Form Helper

public function index()
{
helper('form');

echo form_open('products/save');
echo form_input('name', 'Product Name');
echo form_submit('submit', 'Save');
echo form_close();
}

👉 Instead of writing long HTML forms, helpers give you shortcuts.

Creating a Custom Helper in CodeIgniter

You can create your own helpers if you need custom utility functions.

Example: app/Helpers/custom_helper.php


<?php
function greet($name)
{
return "Hello, " . ucfirst($name) . "!";
}

Load & Use:

public function index()
{
helper('custom');
echo greet('john'); // Output: Hello, John!
}

Advantages of Helpers

  • Simple to use — just load and call functions.
  • Keeps code DRY (Don’t Repeat Yourself).
  • Faster development for common repetitive tasks.
  • Easy to create your own helpers.