what is helper class?

How to create and use helper class in codeigniter application?

The helper is used as a function library, by this we can just load a helper class in the controller or view as well. CodeIgniter provide different types of helper class, such as url_helper, captcha_helper ,email_helper etc. They all are located in system/helper.


Each helper file is simply a collection of functions in a particular category. 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.


Each helper function performs one specific task, with no dependence on other functions. CodeIgniter does not load Helper function by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.


Helpers are typically stored in your system/helpers, or 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.

How to load a Helper file in your controller class?

First create a helper file in your helpers directory under application folder.

common_helper.php

File name : index.php

$this->load->helper('common');
here common is the name of the helper file, without the .php file extension or the "_helper" part.

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

$this->load->helper('url');
current_url()
Returns the full URL (including segments) of the page being currently viewed. Example:

echo current_url();

// echoes 'http://itechtuto.com/contact'

A helper can be loaded anywhere within your controller methods or view page. 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.

uri_string()
Returns the URI segments of any page that contains this function. For example, if your URL was this:

http://itechtuto.com/blog/comments/123
The function would return:

/blog/comments/123

File name : index.php


Html Img helper :-

File name : index.php

img()
Lets you create HTML tags. The first parameter contains the image source. Example:

echo img('images/picture.jpg');
// gives
Additionally, an associative array can be passed to the img() function for complete control over all attributes and values. If an alt attribute is not provided, CodeIgniter will generate an empty string.

$image_properties = array(
'src' => 'images/picture.jpg',
'alt' => 'Image description',
'class' => 'image_class',
'width' => '200',
'height' => '200',
'title' => 'That was quite a night'
);

img($image_properties);

Array Helper

The Array Helper file contains functions that assist in working with arrays.

Loading this Helper

This helper is loaded using the following code:
$this->load->helper('array');

How to get random element from array.

File name : index.php

$quotes = array(
"I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
"Don't stay in bed, unless you can make money in bed. - George Burns",
"We didn't lose the game; we just ran out of time. - Vince Lombardi",
"If everything seems under control, you're not going fast enough. - Mario Andretti",
"Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
"Chance favors the prepared mind - Louis Pasteur"
);

echo random_element($quotes);

How to create custom helper in codeigniter.

CodeIgniter helper file is a collection of functions, it help you to do task.

Now we will create a custom helper file and a function in this helper file. Also we will use this function in controller and views.

At first we will create custom_helper.php file in application/helpers directory. The filename always would have a _helper suffix. Now create get_user_details() function, this function takes user ID argument and return the respective user details. We will use the get_instance() function for access CodeIgniter’s native resources. get_instance() function returns the main CodeIgniter object. We have assign it into the $ci variable and it will help to using CodeIgniter database functions.

File name : index.php


if ( ! function_exists('get_user_details')){
function get_user_details($user_id){
//get main CodeIgniter object
$ci =& get_instance();

//load databse library
$ci->load->database();

//get data from database
$query = $ci->db->get_where('users',array('id'=>$id));

if($query->num_rows() > 0){
$result = $query->row_array();
return $result;
}else{
return false;
}
}
}

We will use get_user_details() custom function in the controller and view. You should load the helper file first for use helper functions. Where “custom” is file name of the helper name, without the .php extension and the “_helper” suffix.

//load custom helper
$this->load->helper('custom');
Now we are able to use helper function in controller and views. If we pass user ID into this function, it will be returned the respective user details.

$user_details = get_user_details(1);

File name : index.php


Fatal error: Call to undefined function base_url()

getting an error because I did not load the url helper to access that function.

I put url in autoload file and you will find the autoload.php in following path application/config/autoload.php.

$autoload['helper'] = array('url');
Save your autoload.php file and run your application, base_url() function will work now.

Alternately, you can add following line of code somewhere in controller :

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

How to call Controller in Helper file In Codeigniter

create a function in controller_load in helper file with name controller_load when you will load controller that time you have to pass absolute path and create object of that class file it will create instance and behave whatever you want to use that controller features.

File Name :

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

if (!function_exists('controller_load'))
{
function controller_load($controller, $method = 'index')
{
require_once(FCPATH . APPPATH . 'controllers/' . $controller . '.php');

$controller = new $controller();

return $controller->$method();
}
}

Step Two :

now you are ready to load controller anywhere. First need to load helper file and then load function which inside helper and pass controller and function which you want to load in your page or controller.

File Name :

$this->load->helper('controller_load');
load_controller('homecontroller', 'view_name');





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here