Monday, July 18, 2011

CodeIgniter - Routing

Hi,

Implementation of the routing is the interesting idea to manage short URL re-writing.

Way to implement the routing :
Just open the application/config/routes.php, insert the line at the end of the file.

1) if you want that URL would be http://example.com/work/holiday
here, work is the controller name and holiday is the data that would be dynamically changed. So, to implement this just write like this:
$route['work/(:any)'] = 'work/work_detail/$1';
here (:any) = would accept any type of argument,
work/work_detail : class/function relationship
$1 : argument passed

class Implementation:

class Work extends CI_Controller{

function __construct(){
parent::__construct();
}
function work_detail($arg){
echo $arg; //would return holiday
}

2) If your URL is http://example.com/sevice/data, where service is controller and data would dynamic changed,
but client requirement is URL : http://example.com/data only. then you have to manager class/function relationship implicitly.

open routes.php and insert line at the end of the file.
$route['^(?!work|home).*'] = 'page/info/$1';

here, i used regular expression to bypass control from work,home class, means url : example.com/work and example.com/home not there then it would follow page class.

i.e. if URL : http://example.com/data called then specific routing will transfer control to the page/info :: class/function relationship,

it will help in short URL re-writing cases.
Implementation:
Class Page extends CI_Controller{
function __construct(){
parent::__construct();
}

function info($arg){
echo $arg; //would give data
}
}


Bye everybody...

No comments :