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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
public function index() {
// Load form helper
$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
<?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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
public function index() {
// Load custom helper
$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
<?php
// Add a function in array inbuilt helper
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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
public function index() {
$this->load->helper('array');
// Call the function
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
<?php
// Override the element function of array helper.
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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
public function index() {
$this->load->helper('array');
// Define array for element function
$arr= ['abc'=>'ABC', 'xyz'=>'XYZ'];
// Call element function but it won't
// give the result of element function from
// inbuilt array helper exist in system folder
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
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
Introduction of Firewall in Computer Network A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa
10 min read