If you use CodeIgniter for your development, you often come to a point wherein you ask yourself – How to create user friendly URLs? In this post, we will tell you, how you can create User friendly URLs through CodeIgniter. But first we need to understand how CodeIgniter creates the URL and the approach that it follows.
CodeIgniter uses a segment-based URI approach rather than the conventional standard “query string” approach to URLs.
What are URI Segments?
The segments that represents the URL e.g. example.com/class/function/ID
- The first segment represents the controller class that should be invoked.
- The second segment represents the class function, or method, that should be called.
- The third, and any additional segments, represent the ID and any variables that will be passed to the controller.
We can remapped the URL , how we want using the URI Routing feature. The URI Class and the URL Helper contain functions that make it easy to work with our URI data.
Adding a URL Suffix
In config/config.php file we can specify a suffix that will be added to all URLs generated by CodeIgniter. For example, if a URL is this:
We can optionally add a suffix, like .html, making the page appear to be of a certain type:
Enabling Query Strings
CodeIgniter optionally supports this capability, which can be enabled in application/config.php file. If you open your config file you’ll see these items:
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = ‘c’;
$config['function_trigger'] = ‘m’;
If you change “enable_query_strings” to TRUE this feature will become active. Your controllers and functions will then be accessible using the “trigger” words you’ve set to invoke your controllers and methods:
URI Routing: Setting your own routing rules
Routing rules are defined in application/config/routes.php file. You’ll see an array called $route that permits you to specify your own routing criteria. Routes can either be specified using wildcards or Regular Expressions
1. Wildcards – There are two types of wildcards are used
- :num – :num will match a segment containing only numbers.
- :any – :any will match a segment containing any character.
A typical wildcard route might look something like this:
$route['product/:num'] = “catalog/product_lookup”;
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 “product” is found in the first segment of the URL, and a number is found in the second segment, the “catalog” class and the “product_lookup” method are instead used.
Here are a few routing examples:
$route['blog/sat'] = “blogs/users/34″;
A URL containing the segments blog/sat will be remapped to the “blogs” class and the “users” method. The ID will be set to “34″.
$route['product/:any'] = “catalog/product_lookup”;
A URL with “product” as the first segment, and anything in the second will be remapped to the “catalog” class and the “product_lookup” method.
$route['product/(:num)'] = “catalog/product_lookup_by_id/$1″;
A URL with “product” as the first segment, and anything in the second will be remapped to the “catalog” class and the “product_lookup_by_id” method passing in the match as a variable to the function.
2. Regular Expressions – We can use regular expressions to define routing rules. Any valid regular expression is allowed. A typical RegEx route might look something like this:
$route['products/mobile/([a-z]+)/(\d+)’] = “$1/id_$2″;
In the above example, a URI similar to products/mobile/123 would instead call the mobile controller class and the id_123 function.