agyanadda@gmail.com

CodeIgniter 4 Helpers

CodeIgniter 4 Basics 1 min read Updated Dec 3, 2026 Topic 9 of 30

Helpers are files of simple functions for common tasks. Load them before use.

Loading helpers

  • In a controller: helper('form'); or helper(['form', 'text']);
  • For every controller: add them to protected $helpers = ['form', 'url']; in BaseController

URL helper (always loaded)

  • base_url('css/style.css') — link to files in public
  • site_url('posts') — link to a route
  • url_to('posts') — URL of a named route
  • current_url(), previous_url()
  • anchor('posts', 'All posts') — a full <a> tag

Form helper

  • form_open('posts/save') — opening <form> tag, adds the CSRF field automatically when CSRF is on
  • form_input(), form_dropdown(), form_close()
  • set_value('title') — refill a field after a failed submit

Other helpers

text (word_limiter, character_limiter), number (number_to_currency), date, filesystem, cookie, inflector.

Your own helper

Create app/Helpers/site_helper.php with plain functions, then load it with helper('site').

Example

Example — Custom helper and URL helpers PHP
<?php
// app/Helpers/site_helper.php
if (! function_exists('rupees')) {
    function rupees(float $amount): string
    {
        return '₹' . number_format($amount, 2);
    }
}

// app/Controllers/BaseController.php
protected $helpers = ['form', 'text', 'site'];

// In any view:
// <link rel="stylesheet" href="<?= base_url('css/style.css') ?>">
// <?= anchor('posts', 'All posts') ?>
// <p>Price: <?= rupees(1499) ?></p>
// <p><?= word_limiter($post['body'], 20) ?></p>

rupees(1499) prints ₹1,499.00. anchor() prints <a href="http://localhost:8080/posts">All posts</a>. word_limiter() shortens the text to 20 words.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which function links to a file in the public folder?

Exercise 2 True / False

Helpers listed in BaseController's $helpers are loaded for every controller.

Exercise 3 Fill in the Blank

A custom helper file must end with .php

Frequently Asked Questions

base_url() builds a URL to a file in the public folder, such as CSS or images. site_url() builds a URL to a page through index.php routing. If you have removed index.php from URLs (indexPage = ''), they look the same, but use each for its purpose.