Monday, July 18, 2011

CodeIgniter - Routing


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
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(){
function work_detail($arg){
echo $arg; //would return holiday

2) If your URL is, where service is controller and data would dynamic changed,
but client requirement is URL : 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 : and not there then it would follow page class.

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

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

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

Bye everybody...

No comments :