Codeigniter - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Helper

CodeIgniter Helper

shape Description

CodeIgniter Helper is a group of functions that provide shortcuts in certain sections that helps in various tasks of the code. Like every other programming language, Helpers are not in Object Oriented format. They have simple, procedural functions. These helper function does not depend on any other functions while executing the tasks.

Loading Helper

shape Description

In CodeIgniter, there are more than 20 helpers which resides in the folder application/helpers. By default, the files of CodeIgniter Helper files are not loaded. In order to use a Helper load it initially from application/helpers folder. If not found, CodeIgniter checks in system/helpers folder which makes helpers available globally to Controllers and Views. Normally, the best practice to load helper is from a controller as shown below.
$this->load->helper('name');

URL Helper

shape Description

CodeIgniter has more than 20 built-in helpers. A new Controller can be created or existing controller can be used for importing one of the built-in Codeigniter Helpers. Presently in this tutorial, URL helper is loaded and used. Step-1 : Open welcome.php file located in application/controllers. Write the following code in index() function which is provided by default.
$this->load->helper("url");
Step-2 : Next open controller view file welcome_message.php located at application/views folder and add the below code. [html] <div id="container"> <h1>Welcome to CodeIgniter!</h1> <div id="body"> <p>Base URL : <?=base_url()?></p> <p>Site URL : <?=site_url()?></p> </div> </div> [/html] The output is as follows:

URL Helper Function

Function Description
base_url() The base URL is returned by this function.
site_url() The site URL is returned by this function.
current_url The present URL full path is returned by this function.
anchor() Standard HTML anchor link is created by this function on the local site URL. It has three parameters text,uri segments and attributes.
anchor_popup() A popup is created in a new window by this function.
mailto() Email can be sent directly from a script using this function and it can be visible in view source. A standard output with HTML tag appears
safe_mailto() Emails can be sent by encoding it as a link address to avoid spam bots.
auto_link() This function will convert the Emails and URL's into links.
prep_url() This function adds 'http://' in the front of website url.
redirect() The function moves the given url to specified location.

Summary

shape Key Points

  • CodeIgniter Helper is a group of functions that provide shortcuts.
  • To load a helper '$this->load->helper(‘name’);' must be used in controller file.
  • The URL Helper has many functions that helps in working with URLs.