- Seme Framework
- Version 3.1.0
- URI Routing
- CLI (command line interface)
- Error & Troubleshooting
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
In some instances, however, you may want to remap this relationship so that a different class/function can be called instead of the one corresponding to the URL.
For example, lets say you want your URLs to have this prototype:
Normally the second segment of the URL is reserved for the function name, but in the example above it instead has a product ID. To overcome this, Seme Framework allows you to remap the URI handler.
Setting custom URI Routing
Routing rules are defined in your app/config/controller.php file. In it you'll see an array called $routes that permits you to specify your own routing criteria. Routes can either be specified using wildcards
A typical wildcard route might look something like this:
$routes['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.
You can match literal values or you can use two wildcard types:
(:num)will match a segment containing only numbers.
(:any)will match a segment containing any character.
Here are a few routing examples:
$routes['journals'] = "blogs";
A URL containing the word "journals" in the first segment will be remapped to the "blogs" class.
$routes['blog/joe'] = "blogs/users/34";
A URL containing the segments blog/joe will be remapped to the "blogs" class and the "users" method. The ID will be set to "34".
$routes['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.
$routes['product/(:num)'] = "catalog/product_lookup_by_id/$1";
A URL with "product" as the first segment, and a number 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.
There are two reserved routes:
$routes['home'] = 'home';
This route indicates which controller class should be loaded if the URI contains no data, which will be the case when people load your root URL. In the above example, the "welcome" class would be loaded. You are encouraged to always have a default route otherwise a 404 page will appear by default.
$routes['notfound'] = 'notfound';
This route indicates which controller class should be loaded if the requested controller is not found. By default it will executed
app/controller/notfound.php. You can change it if necessary.
Another reserved route is admin secret route. Please refer to this docs for using admin secret routing.