Uniform Resource Identifier or URI consists of a string of character used to name a resource on the Internet. The name of a resource on the Internet should be naming conventional so that Search Engine like google , yahoo, ask etc. can easily find out. And it can be possible if we arrange the URI perfectly following some rules of URI Routing. In CodeIgniter the segments in a URI normally follow a pattern like this –
http://www.mysite.com/myclass/myfunction/myparam
Suppose that we have a URL like below –
http://localhost/TestingServer/home/chidc/mid/4/sid/11/chid/12
where “http://localhost/TestingServer/” is our BASE URL and others are segments. If we describe the above URI following the CodeIgniter pattern –
http://localhost/TestingServer/home(myclass)/chidc(myfunction)/mid(paramname1)/4(paramvalue1)/sid(paramname2)/11(paramvalue2)/chid(paramname3)/12(paramvalue3)
Now we will apply our own routing rules to the above URL.
In CodeIgniter routing rules are defined in application/config/routes.php file.
// routes.php
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
/*
| ————————————————————————-
| URI ROUTING
| ————————————————————————-
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| ————————————————————————-
| RESERVED ROUTES
| ————————————————————————-
|
| There are two reserved routes:
|
| $route[‘default_controller’] = ‘welcome’;
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the “welcome” class
| would be loaded.
|
| $route[‘scaffolding_trigger’] = ‘scaffolding’;
|
| This route lets you set a “secret” word that will trigger the
| scaffolding feature for added security. Note: Scaffolding must be
| enabled in the controller in which you intend to use it. The reserved
| routes must come before any wildcard or regular expression routes.
|
*/
$route[‘default_controller’] = “home”;
$route[‘scaffolding_trigger’] = “”;
/* End of file routes.php */
/* Location: ./system/application/config/routes.php */
?>
Here $route is an array by which we can specify our own routing rules. Now we are going to specify our own routing rules below –
$route[‘childmenu/([0-9]+)/([0-9]+)/([0-9]+)’] = “home/chidc/mid/$1/sid/$2/chid/$3”;
Here segments childmenu and regular expressions will be remapped to the ‘home’ class , ‘chidc’ function and three params value ‘mid’, ‘sid’, ‘chid’.
By adding above code to routes.php file –
// modified routes.php
<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
/*
| ————————————————————————-
| URI ROUTING
| ————————————————————————-
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| ————————————————————————-
| RESERVED ROUTES
| ————————————————————————-
|
| There are two reserved routes:
|
| $route[‘default_controller’] = ‘welcome’;
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the “welcome” class
| would be loaded.
|
| $route[‘scaffolding_trigger’] = ‘scaffolding’;
|
| This route lets you set a “secret” word that will trigger the
| scaffolding feature for added security. Note: Scaffolding must be
| enabled in the controller in which you intend to use it. The reserved
| routes must come before any wildcard or regular expression routes.
|
*/
$route[‘default_controller’] = “home”;
$route[‘scaffolding_trigger’] = “”;
// Customized routing rules
$route[‘childmenu/([0-9]+)/([0-9]+)/([0-9]+)’] = “home/chidc/mid/$1/sid/$2/chid/$3″;
/* End of file routes.php */
/* Location: ./system/application/config/routes.php */
?>
Below we will go to the finished step for view –
// view.php
<a href=”<?=base_url()?>childmenu/<?=$id1?>/<?=$id2?>/<?=$id3?>”>Menu name</a>
[ It may help you 🙂 ]