What is helpers in CodeIgniter and how to load helper file?

105 Views

What is helpers in CodeIgniter and how to load helper file?

CodeIgniter | What is helpers | Load helper file

Helper is simply a collection of function in a particular category which help you with tasks. The helper file can be loaded in controller constructor, some specific function or in application/config/autoload.php file

Adding helper in controller:

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

Note: The name is the file name of the helper, without the .php file extension or the "helper" part.

There are several helper classes already available in codeigniter framework. There are URL Helpers, that assist in creating links, there are Form Helpers that help you create form elements, Text Helpers perform various text formatting routines, Cookie Helpers set and read cookies, File Helpers help you deal with files, etc.

The helpers in codeigniter are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.

CodeIgniter does not load Helper Files by default, you need to load it and once loaded, it becomes globally available in your controller and views.

The system helpers are stored in system/helpers directory, and user defined helpers are stored in application/helpers directory. CodeIgniter will look first in your application/helpers directory. If the directory does not exist or the specified helper is not located there CI will instead look in your global system/helpers/ directory.

For example, to load the URL Helper file, which is named url_helper.php, you would do this:

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

A helper can be loaded anywhere in your controller methods (or even within your View files, although that’s not a good practice). You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.

 


 

Helper loading in controller constructor:

 <?php

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

class Test extends My_Controller {

    public function __construct() {
        parent:: __construct();
		
		// loading helper
        $this->load->helper('url');
		
	}
    

    public function index(){
    
        // site_url() is a helper function
        echo site_url();
		
	}
}

?>

 


 

Helper loading in a function

 <?php

public function index(){

    // loading text helper in the same function
    $this->load->helper('text');

    $string = "Here is a nice text string consisting of eleven words.";
	
	// word_limiter() is a helper function
    $string = word_limiter($string, 4);
	
}
    
	
    
	// Returns:  Here is a nice

?>

 


 

Loading Multiple Helpers at once

If you need to load more than one helper you can specify them in an array, like this:

 <?php

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

?>

 


 

Loading helper in autoload.php file

You can auto-load helper during system initialization by adding the helper to the autoload array in application/config/autoload.php file .

 <?php

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

?>

 

Tags:-
Codeigniter