Helpers are the reusable code in codeignitor like libraries. The only difference is that libraries are collection of classes whereas helper is defined as individual independent set of functions. Helper functions need to be loaded before using it. We can find all the helpers in codeignitor documentation Codeignitor Helpers and that can be used depends on kind of requirement.
Create a controller users.php and then we can use below code to use helper.
Controller: users.php
<?php
defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
class Users extends CI_Controller {
public function index() {
$this ->load->helper( 'form' );
}
}
?>
|
If we need to load multiple helpers, we can create an array and then we can define all the helper’s name in that array.
$this->load->helper(array('form', 'email', 'url'));
Custom Helpers: Codeignitor has already a lot of built-in helpers but if we need to create a function which is not in the helper then we can create our own custom helper and use it in the same way like inbuilt helpers. Inbuilt helpers are available in system folder but custom helper needs to be created in application/helpers folder. Create a file abc_helper.php in application/helpers folder. Below is the example of creating functionality in our custom helper.
Custom Helper: abc_helper.php
<?php
function test() {
echo "Custom helper for codeignitor" ;
}
?>
|
Custom helpers can be used just like inbuilt helpers in the controller. So in our users.php controller use the code below to check this.
Controller: users.php
<?php
defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
class Users extends CI_Controller {
public function index() {
$this ->load->helper( 'abc' );
test();
}
}
?
|
Output:
Custom helper for codeignitor
Extending Helpers: If we want to add some other function in already inbuilt helpers, we can do that easily. Extending a helper means adding more function in inbuilt helpers or overriding inbuilt functions. It is also created in the same folder application/helpers just like we were doing for custom helpers. Now here we need to keep in mind that we need to add ‘MY_’ prefix when we give the name for our helper file. So create a file MY_array_helper.php in application/helpers folder and use the code below.
Extended Helper: MY_array_helper.php
<?php
function test() {
echo "Extend array helper in codeignitor" ;
}
?>
|
Once the array helper is extended, we can use its additional function ‘test’ in our controller. So in controller users.php use the code below to call ‘test’ function from array helper and then we will be able to use our own function.
Controller: users.php
<?php
defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
class Users extends CI_Controller {
public function index() {
$this ->load->helper( 'array' );
test();
}
}
?>
|
Output:
Extend array helper in codeignitor
Till now we were extending our helpers and adding our own functionality, now if want to override a function of the inbuilt helper, we can also do that. To override a function from the inbuilt helper, we need to give the same function name which already exists in that helper. So, create a file MY_array_helper.php in application/helpers folder and create a function ‘element’ which already exists in system/helpers/array_helper.php.
Extended Helper: MY_array_helper.php
<?php
function element() {
echo "Overridden extended array helper in codeignitor" ;
}
?>
|
Now in our controller users.php, if we will load array helper and call ‘element’ function it will give the result of our overridden function of array helper instead of ‘element’ function of system/helpers/array_helper.php. It will completely override the functionality of array helper element function and will give a new result which we have defined in ‘MY_array_helper.php’.
Controller: users.php
<?php
defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' );
class Users extends CI_Controller {
public function index() {
$this ->load->helper( 'array' );
$arr = [ 'abc' => 'ABC' , 'xyz' => 'XYZ' ];
echo element( 'abc' , $arr , 'Not Found' );
}
}
?>
|
Output:
Overridden extended array helper in codeignitor
So from the above example its clear that once we override the function of the inbuilt helper array it gives us the result of our own defined ‘element’ function in MY_array_helper.php instead of older one ‘element’ function in array helper. If we will delete our file MY_array_helper.php, we will get the result of ‘element’ function which exists in system/helpers/array_helper.php
Output:
ABC
Similar Reads
Libraries in Codeignitor
Any kind of framework is a collection of reusable code which is divided into classes, so if we talk about OOPS or MVC, we do all our task in classes like for email sending, form validation or pagination kind of work. Collection of these classes are called a library in Codeignitor. There are so many
5 min read
How MVC works in Codeignitor ?
Codeignitor is based on MVC design pattern. We will see how the controller, model, and view work and display the result to the user. Controller: Like the name suggest, it controls the communication between views and models. All the URL works in CodeIgniter with controller name. Now let's see some po
4 min read
How to check the CodeIgniter version ?
PHP has many frameworks like Laravel, CodeIgniter, etc., but "Codeignitor" is one of the popular MVC frameworks of PHP. Most of the developers prefer to make their projects on Codeignitor because it is very lightweight and also its documentation is easy to understand. Some of the features and advant
4 min read
Introduction to Codeignitor (PHP)
Codeignitor is one of the popular MVC framework of PHP. Most of the developers prefer to make their projects on Codeignitor because of it's lightweight and easy to understand documentation. Some of the features, advantages or why use Codeignitor is given below. Why use Codeignitor? Fast and lightwei
4 min read
Explain CodeIgniter folder structure
CodeIgniter is an Application Development Framework to build websites using PHP. It is used to minimize the code while developing an application and developed as much fast. The folder structure is an important part of CodeIgniter. It is important to understand the file structure in CodeIgniter to de
3 min read
Spectre Inline code
The HTML code element is used to style the inline code and the code snippet. In this article, we will learn about the Inline code. Inline code is a line of the actual code. For inline code, you can use the <code> element. Spectre Inline code class: There is no predefined class for this, we can
1 min read
Spectre Code snippet
The code element is used to style the inline code and the code snippet. In this article, we will learn about the code snippet. The code snippet is the part of the actual code that contains multiple lines of code, so we can use <pre> with the code class as a container, and add <code> insi
1 min read
Foundation CSS Typography Helpers
Foundation CSS is one of the most popular front-end frameworks used by developers to design the website. It has several useful components to make the website look much more professional and user-friendly. One such component is the Typography helper. In every website, typography is an essential thing
5 min read
HTML Computer Code Elements
HTML provides a set of elements tailored for displaying computer code so that it is easily distinguishable from other text on a webpage. These elements help in formatting and presenting source code in a readable and syntactically correct manner. Table of Content The code TagThe kbd TagThe pre TagThe
5 min read
WordPress Edit Comments
WordPress is a popular platform for creating and managing websites, and it offers powerful tools for managing comments. Being able to edit comments in WordPress is crucial for maintaining the quality and relevance of discussions on your site. This article will guide you on how to easily edit comment
4 min read