Advertisement

Codeigniter

CodeIgniter Helpers

Codeigniter | Framework | Helpers | Mvc | PHP | URL

Codeigniter Helpers

Advertisement

This entry is part 5 of 8 in the series Learn Codeigniter

Codeigniter Helpers are utility functions categorised in different helper files. Each helper contains bunch of functions that are related to each other. For example, One helper that I always includes in my project is URL Helper, which contains functions like url_title, site_url, base_url, mailto, etc. All those functions are related to URLs, so they placed inside URL Helper. Codeigniter Helpers are very easy to use, if you familiar with codeigniter controllers & views. If you are new to Codeigniter please start with full course on Learn CodeIgniter.

Download Source Code Files

 

CodeIgniter Helpers : Working with Built-in Helpers

CodeIgniter comes with 20+ helpers built-in its framework. We can use those built-in codeigniter helpers in our website. Create Controller or use your existing controller for importing one of the built-in Codeigniter Helpers. We are right now using URL helper for demo purpose.

Step 1. In Controller file inside function (in my case its index function but its up to you where you are using helper) place following highlighted code.

Step 2. Open view file for your controller’s action (for me its welcome_message view located at views folder) and add following code inside view file. Because this is a demo for helpers I am using basic HTML.

As you can see highlighted code contains usage of URL Helper functions like base_url, site_url, url_title & mailto. URL helper contains more functions than used here so if you want to know just find file for URL helper inside ‘system/helpers/url_helper.php’.

Lets open browser and navigate to controller’s action we are working on. For me its http://localhost:8888/CI/index.php/welcome. But because I configured my welcome controller as a default controller in routes.php config file & I have put code inside index which is default action for codeigniter. I have to navigate to http://localhost:8888/CI/. Here is what I get in my browser.

Codeigniter Helpers Output

 

As you can see only difference between base_url & site_url is the inclusion of index.php in output. url_title function outputs slug for given string by eliminating spaces & symbols used in string.

mailto function accepts 2 parameters:

  1. email
  2. string to display for link

Now as you can see URL_Helper has great usage on project level. so we have to include it in every controller’s every function. But we can move $this->load->helper(“url”) line to constructor of controller so we have to write it once in every controller like below code.

This is nice but we still repeating same line in every controller. So it’s time to be smart by removing load helper line from every controller and follow next step.

Step 3 After removing load helper code from every controller, Open autoload.php file located inside application/config directory. and find the following line or similar to following line.

And Change it to

And that’s it. Now we can use functions located inside url_helper everywhere in our project. For more information about Codeigniter Helpers leave a comment for us so we can work on it.

 

Download Source Code Files

If you like this article about Codeigniter Helpers, please hit like & leave a comment for us. If you have any issue please feel free to mention same in comment so we can serve you better. Visit & like our Facebook Fan Page to get constant update of our website.

Series Navigation<< Codeigniter create helperHow to send email in Codeigniter email Library >>

Bookmark Link using: bookmark at folkd

  • Dhayal Ram

    Thanks for the sorce

    • Ronak Prajapati

      welcome :)