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,
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, http://<domain/my_custom_slug_rule/param1 then I should be define it as,

$route['my_custom_slug_rule'] = "controller/method";

Then how about the parameters? Now for this we have wildcard rule. And we will use either ‘(:num)’ or (:’any)’ as wildcard. Now the route looks like,

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

  1. $route[‘default_controller’] -> To call the default controller when user opens the application.
  2. $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 404 page to the user. 


Submit a Comment

Your email address will not be published. Required fields are marked *