Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Codeigniter URL Routing Sample Questions and Answers

on 2011/07/29 7:24 AM stored in: Uncategorized and tagged:

Introduction To Codeigniter URL Routing

1

What is the default URL pattern used in Codeigniter framework ?

Codeigniter framework URL has 4 main segments in default url pattern. First we have the server name and next we have the controller class name followed by controller function name and function parameters at the end. Codeigniter url segments can be accessed using the url helper. Sample URL is given below.
http://servername/controllerName/controllerFunction/parameter1/parameter2.

2

What is Codeigniter URL Routing ?

Routing in codeigniter means that we can customize the default URL pattern to use our own url pattern according to our requirement. Once configured whenever request comes and matches our url pattern it will direct to the specified controller and function automatically.

3

Why we need to change the URL routes ?

  • Make seo friendly urls to optimize the pages for search engines to get more user visits.
  • Hide some url component like controller name,function name from the users for enhance security.
  • Provide different functionality to specific parts of a system.
4

How can we define custom routing rules?

Codeigniter provides a file called routes.php located at the config folder to define custom routes to your application.

Codeigniter Routes Common Examples
1

Removing the controller class name from URL ?

This is effective when you have only one controller with all the functions. Since there is only one controller you can include the function name and parameters in the url and redirect to the available single controller by configuring settings in routes file. Including the following code line in routes files will redirect all requests to controller called “Blog”.

http://www.example.com/functionName/parameters

$route['(:any)'] = "blog/$1";

Requests with any function name is directed to the appropriate function in blog controller. $1 will contain function name and function parameters.

2

Removing the controller class and function name from URL ?

This is very useful if you are developing a cms with codeigniter. URL’s in cms should be user friendly and seo friendly. We normally maintain the page name of a cms as the parameter. We can optimize pages for search engines if we include the page name right after the domain name. This can be achieved by using routes in CI to remove both controller and function from url.

http://www.example.com/pageName

$route['(:any)'] = "blog/display_page";

All the url’s contain the page name only. So all the pages will be directed to display_page function of blog controller. If you are using this code you can only call one controller and one function name. If you have other controllers to call you have to setup routing rules for them before using above code.

Note : It’s important to consider the precedence of routing rules if you are using filters like (:any) or (:num). Rules defined higher in the file executes first. Using 2 filters without considering all types of url in your application may cause conflicts in routing
Whats Next ?
I have provided frequently identified questions and answers regarding codeigniter routing. More questions and answers in this section are coming soon. If you have any questions regarding codeigniter routing techniques,please use the comment section so that i can update this post with new questions

5 Responses to “Codeigniter URL Routing Sample Questions and Answers”

  1. Steve Lemaire Says:

    Very well written post. Thank you

  2. Nish Says:

    I think very good start on CodeIgniter articles.

  3. air max 180 Says:

    Good chooce to wright about this. Even if it’s not a suprising new thing. Next to that it is more the way how youre view on it is. I’m glad not everyone has the same way off wrighting about it. So thanks a lot i injoyed it.

  4. Design Says:

    Hello, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can recommend? I get so much lately it’s driving me crazy so any assistance is very much appreciated.

  5. admin Says:

    I am facing this problem too. I would suggest you to use a captcha image for user verification in form. Also there are some spam preventing plugins.But i haven’t use them yet.

Leave a Reply

Follow Us On Facebook