Helpers in CodeIgniter
×

Helpers in CodeIgniter

2317

  • In Codeigniter, a helper is a collection of independent procedural functions in a particular category.
  • Each helper function performs one specific task, with no dependence on other functions.

Related Topic:

Models in Codeigniter
Codeigniter Interview Questions

Different types of CodeIgniter Helpers

There are three types of helpers:

1 Built-in helpers: These helpers are stored in the system/helpers directory. The Codeigniter developers community network provides many built-in helpers which cover a rich set of topics.

  • Array Helper
  • CAPTCHA Helper
  • Cookie Helper
  • Date Helper
  • Directory Helper
  • Download Helper
  • Email Helper
  • File Helper
  • Form Helper
  • HTML Helper
  • Inflector Helper
  • Language Helper
  • Number Helper
  • Path Helper
  • Security Helper
  • Smiley Helper
  • String Helper
  • Text Helper
  • Typography Helper
  • URL Helper
  • XML Helper

  • 2 Third-party helpers: These are third-party helpers through which we can integrate third-party facilities in our application. These helpers are stored in the application/helpers directory.

  • ssl_helper.php
  • html_manipulator_helper.php


Related Topics:

Codeigniter Introduction
Codeigniter features

3 Custom helpers: These helpers are created into the application/helpers directory. The helper file must be in the following format:

application/helpers/helper_name_helper.php

Where helper_name is the name of the helper which we want to create.


Loading a helper:

We can load a helper automatically or by the controller.

  • We can load a helper automatically by application/config/autoload.php file. Go to the given path, open the autoload.php file and write the given line.
  • $autoload['helper'] = array('helper_name');

    Where helper_name is the name of helper which we want to load.

  • To loading a helper into a certain controller use the following lines.
$this->load->helper('helper_name');

Where helper_name is the name of helper which we want to load.

Example: In this example, we will use the URL helper function site_url() which returns the site URL, and base_url() which will return the base URL as specified in the config file.

Put this code into your controller file application/controllers/Demo.php

Demo.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Demo extends CI_Controller{
public function index()
{
$this->load->helper("url"); //loading built-in URL helper
$this->load->view("First_view"); //loading view First_view
}
}
?>

Now, put the below code into the application/views/First_view.php file.

First_view.php

<html>
<head>
<title>First View</title>
</head>
<body>
<h2>This is the very first view of my application</h2>
<a href="<?php echo site_url(); ?>">Click here to go on index page</a>
<br>
<a href="<?php echo base_url(); ?>">Click here to go on CodingTag</a>
</body>
</html>

Now check the result by entering the given URL into the browser.

http://localhost/ci/index.php/Demo

Output:

After clicking on the link, you will reach the index page of your application.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments