URLs in Codeigniter are a short path to redirect users from one page to another. We predefined routes to match and respond to URL requests, if any of the predefined route requests don’t match then Codeigniter throws a 404 exception that says ‘page not found’.
To create a route in our application we need to add routes in application/config/routes.php
$route['default_controller'] = 'welcome'; $route[‘profile’] = 'name'; $route['phone’'] = 'phone/create'; $route['edit/:id'] = 'number’/edit'; $route['update/:id'] = 'number/update'; $route['delete/:id'] = 'number/delete'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE;
Default_controller: Every time we run the application the default route will take us to the welcome page
404_override: when the URL doesn’t match any of the predefined routes the 404_override route will display an exception page
Setting Custom Routing rules:
The routing rules can be found in the route.php file inside the application/config. Folder, under $routes array we can add our custom rules and criteria for routing that can be classified in 2 types, wildcard or regular expression.
Types in wildcard:
num-series: Only contains a series of numbers.
$route['(contact/:num)'] = edit/profile/$1;
It means when we'll pass URL http://www.contact.com/num/1
$1: we can pass any number here.
any-series: Only contains a series of characters.
$route['(contact/:num)'] = edit/profile/;
It means when we'll pass URL http://www.contact.com/num/qwer
Any series of characters can be passed in the last segment.
REGEX, Regular expressions are special strings that describe search patterns
$route['contact'(a-zA-Z0-9]+)'] = edit/profile;
We can add a suffix to our URLs in the config.php file located inside the application/config folder
$config['url_suffix'] = '.abc;
URL before adding a suffix to it:
URL after adding a suffix to it: