Posted by Arjun on Tuesday 15th October 2013

How to Extend CodeIgniter Helper?

CI helper CodeIgniter CodeIgniter Helper

CodeIgniter Helper Function are available anywhere in the code, more than 20 CI Helpers are there(URL, Form, Directory, ...etc). You can Easily Extend CodeIgniter Helpers. CodeIgniter Helper is just a PHP File with the bunch of functions, CI helper is not a class.

You can extend a CodeIgniter helper in a similar way to controllers and models. First, save a file called “MY_[name]_helper.php” to application/helpers where [name] is the name of the helper. To extend the URL helper it would be “MY_url_helper.php”. There is no need to extend any class here as helpers are just a bunch of functions. Just open PHP tags and start writing your functions to this file. Remember not to add a closing PHP tag as it may cause trouble with headers. It’s as simple as:

// save in applications/helpers/MY_url_helper.php
function force_ssl() {
    if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") {
        $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

You load an extended helper the same way you load a normal helper. To load the url helper from a controller (which is the best practice) just use $this->load->helper('url');. This makes your new functions available globally. Remember this means you access them directly (not with $this->url_helper->my_function()).

If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file.

$autoload['helper'] = array('url');