CodeIgniter Helper

 

What is Helper

In CodeIgniter there are helpers which are there to help you with different tasks. Every helper file is a collection of functions aiming towards a particular role. Some of the helpers are 'file helpers' which help you to deal with the file, 'text helpers' to perform various text formatting routines, 'form helpers' to create form element, 'cookie helpers' set and read cookies, 'URL helpers' which assist in creating links, etc.

Helpers are not written in Object Oriented format, instead they are simple, procedural functions, independent of each other.

To use helper files, you need to load it. Once loaded it is globally available to your controller and views. They are located at two places in CodeIgniter. CodeIgniter will look first for a helper in application/helpers folder and if not found there then it will go to system/helpers folder.


Loading a Helper

A helper can be loaded in controller constructor which makes them globally available, or they can also be loaded in specific functions which need them.

It can be loaded with the following code:

  1. $this->load->helper('file_name');  

Write your file name here at the place of file_name.

To load URL helper,

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

You can also auto-load a helper if your application needs that helper globally by adding the helper in application/config/autoload.php file.


Loading Multiple Helpers

To load multiple helpers, specify them in an array,

  1. $this->load->helper(  
  2.         array('helper1', 'helper2', 'helper3')  
  3. );  

html Helper Example

We are going to show you an example of html helper by using it in a basic website page. Here, we'll auto-load our helper.

Go to autoload.php file via application/config/autoload.php

  1. $autoload['helper'] = array('html');  

In the above file, name your helper, here it is html.

In application/controllers there is file Form.php

  1. <?php  
  2. defined('BASEPATH') OR exit('No direct script access allowed');  
  3.   
  4. class Form extends CI_Controller {  
  5.   
  6.     public function index()  
  7.     {  
  8.         $this->load->view('header');  
  9.         $this->load->view('nav');  
  10.         $this->load->view('content');  
  11.         $this->load->view('footer');  
  12.     }  
  13. }     
  14. ?>  

In application/views there is file header.php

The first line is coded in php tag.

  1. <?php echo doctype("html5"); ?>  
  2. <html>  
  3. <head>  
  4.     <title>Basic Site</title>  
  5.     <style type="text/css">  
  6.           
  7.         body, html{margin:0; padding:0;}  
  8.   
  9.         body{  
  10.             background-color:#eee;  
  11.         }  
  12.         h1, h2, h3, h4, p, a, li, ul{  
  13.             font-family: arial, sans-serif;  
  14.             color: black;  
  15.             text-decoration: none;  
  16.         }  
  17.         #nav{  
  18.             margin: 50px auto 0 auto;  
  19.             width: 10000px;  
  20.             background-color: #888;  
  21.             height: 15px;  
  22.             padding: 20px;  
  23.         }  
  24.   
  25.         #nav a:hover{  
  26.             color: red;  
  27.   
  28.         }  
  29.         #nav ul{  
  30.             list-style: none;  
  31.             float: left;  
  32.             margin: 0 50px;  
  33.         }  
  34.   
  35.         #nav ul li{  
  36.             display: inline;  
  37.         }  
  38.   
  39.         #content{  
  40.             width: 1000px;  
  41.             min-height: 100%;  
  42.             margin: 0 auto;  
  43.             padding: 20px;  
  44.         }  
  45.   
  46.         #footer{  
  47.             width: 400px;  
  48.             height: 15px;  
  49.             margin: 0 auto;  
  50.             padding: 20px;  
  51.         }  
  52.   
  53.         #footer p{  
  54.             color: #777;  
  55.         }  
  56.     </style>  
  57.   
  58.   
  59.   
  60. </head>  

The other half coding for file header.php is shown in below snapshot.

In application/views there is file content.php

Here also the heading is written in php tag instead of html.

  1. <div id="content">  
  2.         <?php echo heading("Welcome to my site", 1); ?>  
  3.         <p>In a professional context it often happens that private or corporate clients   
  4.         corder a publication to be made and presented with the actual content still not being ready.  
  5.          Think of a news blog that's filled with content hourly on the day of going live. However,   
  6.          reviewers tend to be distracted by comprehensible content, say, a random text copied from   
  7.          a newspaper or the internet. The are likely to focus on the text, disregarding the layout  
  8.           and its elements. Besides, random text risks to be unintendedly humorous or offensive,  
  9.            an unacceptable risk in corporate environments.</p>  
  10.     </div>  

Final output is just like a normal page as shown below with the URL localhost/helper/index.php/Form.

Comments

Popular posts from this blog

Codeigniter Pro Level Tips and Tricks

Login with Facebook in CodeIgniter

PHP - File Uploading