CodeIgniter Route Configuration
Route configuration generally used for remapping URL string. Typically there is a one-to-one relationship between an URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
example.com/class/function/id/
In some instances, however, you may want to remap this relationship so that a different class/method can be called instead of the one corresponding to the URL.
example.com/user/base_info
example.com/profile/id
Setting your own route configuration rules using following steps:
- Go to your application directory then open routes.php file under config directory( application /config / routes.php ).
- At end of file find
$route
array - Specify your own routeing criteria ($route[‘profile/:num’]=‘user/base_info’;)
Note : Routes can either be specified using wildcards or Regular Expressions.
Wildcards
A typical wildcard route might look something like this:
$route[‘profile/:num’]=‘user/base_info’;
In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to. In the above example, if the literal word “profile” is found in the first segment of the URL, and a number is found in the second segment, the “user” class and the “base_info” method are instead used.
You can match literal values or you can use two wildcard types these are:
- (:num) will match a segment containing only numbers.
- (:any) will match a segment containing any character (except for ‘/’, which is the segment delimiter).
There are three reserved routes:
1) $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.
2) $route[‘404_override’] = ‘errors/page_missing’;
This route will tell the Router which controller/method to use if those provided in the URL cannot be matched to a valid route.
3)$route[‘translate_uri_dashes’] = FALSE;
This is not exactly a route but allows you to automatically route controller and method names that contain dashes. ‘-‘ isn’t a valid class or method name character, so it requires translation. When you set this option to TRUE, it will replace ALL dashes in the controller and method URI segments.
<<Previous Next>>
Subroto Mondal
Latest posts by Subroto Mondal (see all)
- Installing and Configuring OpenShift: A Step-by-Step Guide for Linux Experts with Terminal Code - February 19, 2023
- Sed Command in Linux with Practical Examples - February 19, 2023
- Rsync Command uses with examples - February 19, 2023
One Pingback
CodeIgniter Remove Index php from URL - NHIT Tech Blog