Create Custom Helper In CodeIgniter.

As the name suggest, helper help with tasks. helper is just a collection of functions. In this post we will learn how we can create custom helper in CodeIgniter.

There are two types of helper in CodeIgniter.

  1. System Helper.
  2. Custom Helper.
System Helper.

System Helpers are already defined in CodeIgniter, we don’t need to define it. Simply we have to use it.

There are many system helpers already available in CodeIgniter. For example URL helper that assists in creating links. Form Helper that help in creating a form element. CodeIgniter helpers are not written in object oriented way. They are simply a set of functions.

CodeIgniter first search helper in application/helper directory. If the specified helper does not found then it will look in system/helpers directory.

Custom Helper.

Custom helpers are created by us to reuse the code.

So in this post we will learn how to create custom helper in CodeIgniter. It helps us in code reusability. That is we have to write once and use anywhere. In this example we will declare one languages array in make dropdown from this array. These created dropdown can be used anywhere in our application by just calling the helper functions. We don’t need to write same code again and again .

We have to load the helper before using it. There are two ways to load helpers in CodeIgniter. First,we can load it via autoload array in config folder. Second, we can load it via controller.

How To Load Helper.

//Load Via Autoload Array In Config Folder

$autoload['helper'] = array('helper_name');

//Load Via Controller.

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

So let’s start writing a custom helper in CodeIgniter.

Create Helper File Within application/helpers directory.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    if(!function_exists('custom_helper_ex')){
    function custom_helper_ex(){
        $lang_array = ['C','C++','PHP','JAVA','PYTHON','JAVASCRIPT','SCALA','PERL']; 
        echo "<select name=lang class=form-control>";
        echo "<option value=''>--Select Language--</option>";
        foreach ($lang_array as $lang) {            
            echo "<option value=$lang>$lang</option>";                
        }  
        echo "</select>";
    }
}

?>

In the above helper I have created one array and make a dropdown from this array. Now we can create a dropdown of language anywhere in our application.

How To Use Helper.

Create controller and load helper.

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

class Custom_Helper extends CI_Controller { 

        public function __construct() {		
		parent::__construct();
		$this->load->helper(array('custom_helper'));
	}    
 
        public function index(){

		$this->load->view('custom_helper_view');
	}
}

Create View within application/view directory and put below code.

<html>
<head>
  <title>Custom Helper Example In Codeigniter</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">  
 </head>  
</head>
<body>
<h3 class="text-success" align="center">Custom Helper Example In Codeigniter</h3><br>
<div class="container">   
  <div class="panel-group">
    <div class="panel panel-primary">
     <div class="panel-heading">Custom Helper Example In Codeigniter</div>         
            <div class="panel-body">                 
                    <div class="form-group">
                        <label class="control-label col-sm-2">Upload CSV File:</label>
                        <div class="col-sm-5">      
                           <?=custom_helper_ex() ?>                  
                        </div>
                    </div>             
            </div> 
      </div>
    </div>
</div> 
</body> 
</html>

Conclusion: This is the steps to implement custom helper in CodeIgniter.

Rest API In Codeigniter – 3. Infinite Scroll Pagination Using PHP And JQuery

Exit mobile version