How Routings Works with CodeIgniter

How Routings Works with CodeIgniter

In this tutorial, i will explain how routings works with codeIgniter. Routings basically two ways one is normal php and other way is codeIgniter Framework.

I will explain one by one, first i will explain normal php way.

Normal Behavior of php Files


Routings Works with CodeIgniter

Above diagram have one root directory and three files like abc.php, pqr.php, xyz.php all these files are normal php files. When you browser that file, go to browser and type like 
      
      localhost/abc.php
      localhost/pqr.php
      localhost/xyz.php


CodeIgniter Behavior of php Files

Routings Works with CodeIgniter

Above Diagram also have one root directory, index.php, Application, Controllers, Views, Model and Welcome.php, When you access welcome.php file, go to browser and type like this

          localhost/index.php/welcome

URL Format(Routing)

CodeIgniter URL routing is required why because various reason but most important reason one is search engine optimization(SEO) point of view and other for visitor point of view. Make the URL routing most beneficial way so that it makes highly sense to users as well as any search engine like google, bing and yahoo etc.

By default, URL in CodeIgniter are designed to be search engine and user friendly, codeIgniter uses a segment-based approach.
  • The first segment represents the class called as controller class that should be invoked.
  • The second segment represents the function, or method, that should be called.
  • The third, and any additional segments, represent the parameter that will be passed to the controller.
For example,  localhost/index.php/controller/method/p1/p2/p3

where controller is the, name of the controller, method is the method inside a controller and  p1, p2, p3 are the parameters  to the method.


Code behind this Controller

<?php

     class Controller extends CI_Controller {

public function method($p1, $p2, $p3)
{

}

}

Default Reserved Routes

Go to application/config/ and open routes.php, just scroll down and see the code shown below

$route['default_controller'] = welcome';

Here route, points to the action will be executed, if the URL  does not have data, this accepts welcome controller and index() method by default.

$route['404_override'] = '';

Here route indicates which controller class will be loaded, if the requested controller is not available means requested fails it override the default 404 error page.

$route['translate_uri_dashes'] = FALSE;

This is Boolean value not exactly the route. If you change this option, false to true means that it automatically replace dashe(-)  with underscores in the controller and method URI segments

Create Own Routing Rules For CodeIgniter

For this go to application/config/ and open the routes.php. you can see array $routes that allow you to create own routing criteria by using two methods shown below

  • Wildcards
  • Regular Expressions

How to use Wildcards

There are two types  of wildcards
  • :num—it match only numbers in segment
  • :any—it match only any character

How to use :num Wildcards

$route['employees/:num']  = 'employee/employee_form/$1';

Here a URL appears with employees as the first segment and a number in the second segment will be remapped with the employee controller class and the employee_form method inside employee controller is passing in the match as a variable to the method

How to use :any Wildcards

$route['employees/:any']    = 'employee/employee_form';

Here a URL appears with employees as the first segment and anything in the second segment will be remapped with the employee controller class and the employee_form method inside employee controller.

For example: Followings routes we can defined as per your requirements

$route['employees/edit/(:any)']      = 'employee/employee_form/$1';
$route['employees/delete/(:any)']  = 'employee/employee_delete/$1';
$route['employees/add']                = 'employee/employee_form';
$route['employees']                       = 'employee/employees';
$route['employees']                       = 'employee';

$route['employees'] = 'employee';

Here a URL appears with employees in the first segment will be remapped with the employee controller class. Simply means that user can see employees in the URL but internally employee controller is called.

$route['employees/add'] = 'employee/employee_form';

Here a URL appears with employees in the first segment will be remapped with the employee controller class and add will be remapped with the employee_form method inside the employee controller class. Simply means that user can see employees/add in the URL but internally employee controller and employee_form are called.

$route['employees/update'] = 'employee/employee_update/2';

Here a URL appears with employees in the first segment will be remapped with the employee controller class and update will be remapped with the employee_form method inside the employee controller class and id will be set to 2.

How to use Regular expressions

You can use regular expressions to define own routings rules.

$route['product/([a-Za-Z0-9]+)'] = 'product/jeans';

Above route, any valid regular expression is allowed but special characters are not allowed.





                   I hope, it helped you to understand, how Routings Works with CodeIgniter......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Thanks For Visiting Blog...