CodeIgniter provides a super-easy mechanism to create SEO-friendly or easy readable URL/slug.
In general, basic URL format in Code Igniter will be like, http://<domain>/controller/method/param1…
If you inspect the URL, the controller refers to the name of the controller, the method is a function name in the controller, and param1… are the parameters to the method. So the structure will be like below,
in <controller>.php,
<?php
.
.
.
function method (param1){
----------
----------
}
.
?>
And in CodeIgniter, you can remap the controller/method with a custom name by setting your own URI routing rules. And you can define your rules in config/routes.php
For example, If I want to call the above URL with my custom slug,
$route['my_custom_slug_rule'] = "controller/method";
Then how about the parameters? Now for
If the param1 is an integer value,
$route['my_custom_slug_rule/(:num)'] = "controller/method/$1";
else we can use (:any) like below,
$route['my_custom_slug_rule/(:any)'] = "controller/method/$1";
And CodeIgniter has few pre-reserved routes
- $route[‘default_controller’] -> To call the default controller when user opens the application.
- $route[‘404_override’] -> To call a custom controller when the requested controller was not found. This gives us
a great flexibility to show custom and well designed 404page to the user.
0 Comments