Open In App

How to load multiple helper files in CodeIgniter framework ?

Last Updated : 16 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A helper is considered to be a collection of functions that are aligned under a single particular category. Helpers are easily available in the CodeIgniter framework. They basically deal with procedural functions. It is used to ease up the tasks that are to be performed. The helpers can be loaded in the controllers to facilitate the task operation. The helpers are specified in the environment without mentioning the ‘.php’ extension or the ‘helper’ tag name. Each helper is associated with one particular task and it remains independent of the other helpers and their corresponding operations.

Syntax:

$this->load->helper('helper-name');

There are various types of helpers, for instance, form helpers assist us in creating various form elements. File helpers are used to perform operations with file elements. The helpers can be contained either in the constructor of the used controllers. It may be a part of some function or auto specified in the autoload.php file. Since the helper files are not included by default in the CodeIgniter project, they must be loaded to be granted access. After loading the file, it becomes available globally in the environment views and controllers. 

Approach 1 (Invoking them in controller files): In order to load multiple helper files in the PHP working environment, we can specify them in an array as components where each of the components corresponds to a helper name. 

The helpers can also be invoked in individual lines using the invocation of the single helper in the respective controller constructor. 

$this->load->helper( 'form')

PHP




<?php
$this->load->helper(
        array('helper1', 'helper2', 'helper3')
);
?>


Approach 2 (In autoload file): The helpers can be auto-loaded in the environment while performing the system initialization. The helper can be added to the environment by adding the helper while specifying the autoload array defined in the application/config/autoload.php file.

PHP




<?php
// Specifying the helpers
    $autoload['helper'] = 
      array('helper1','helper2','helper3');
?>


The following code snippet illustrates a snapshot of the autoload.php file in the CodeIgniter framework.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads