Custom Helper
Create custom helper

Create helper and save in application->helpers->new_helper.php

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

if(!function_exists('test_method'))
{
function test_method($var = '')
{
return $var;
}
}

Note: Every helper name ending with _helper

Create controller to run custom helper

Create controller and save in application->controllers->User.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

public function index()
{
$this->load->helper('new_helper');
echo test_method('Hello World');
}

}

When we run this controller like http://localhost/project1/index.php/user

This is print Hello World. because we pass Hello World in test_method and test_method print the variable receive by
function parameter

Extend and overright existing helper function

We can extend any In built helper, In built helper are available in system->helpers.

We can add any function in existing helper
We can overright the existing helper.

so for extending any helper then helper name start with MY and end with _helper. suppose we want extend array helper so
the name of helper will be MY_array_helper

Create customer helper, save in application->helpers->MY_array_helper

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

if(!function_exists('test_method'))
{
function test_method($var = '')
{
return $var;
}
}

function element()
{
echo "This is customer element";
}

Create controller and save in application->controllers->User.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

public function index()
{
$this->load->helper('array');

// Extend helper
echo test_method('Hello World');

// Replace function
echo element();
}

}


Previous
Next