CodeIgniter Helpers

What is a Helper?

In CodeIgniter, a Helper is a collection of standalone functions that help you perform common tasks.

They are not classes — just PHP functions you can call anywhere after loading the helper.

Think of Helpers as "shortcuts" for repetitive tasks like working with URLs, strings, forms, dates, etc.

Where Helpers Live in CodeIgniter

Helpers are stored in:

system/Helpers/    (Core helpers)
app/Helpers/      (Custom helpers you create)


Loading a Helper

You can load a helper in two ways:

1. Load in a Controller

helper('url');

This loads the url_helper.php file so you can use its functions.


2. Auto-load in Config

Edit:

app/Config/Autoload.php

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

This makes the helpers available everywhere without loading them manually.

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

Load the helper:

helper('url');

Use the function:

echo base_url('products/view/10');
// Output: http://your-site.com/products/view/10


Example: Using Form Helper

helper('form');

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


Creating a Custom Helper in CodeIgniter

Example: app/Helpers/custom_helper.php

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

Load & Use:

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.



Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.