- Seme Framework
- Credits
- Version 3.2.X
- Issue
- Deployment
URI Routing
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:
example.com/class/method/id/
However in some instances, 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 like this:
example.com/product/1/
example.com/product/2/
example.com/product/3/
example.com/product/4/
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 Controller Configuration.
You can set this routing depend on your requirements.
And then if you opened the configuration file, you'll see an array called $routes
that permits you to specify your own routing criteria.
Routes can either be specified using wildcards.
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 of the url, so 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.
Examples
Here are a few routing examples:
$routes['produk'] = "products";
An URL containing the word produk
in the first segment will be remapped to the products
class or to the directory
in a controller with same name.
$routes['produk/ojan'] = "products/detail/15";
An URL containing the segments produk/ojan
will be remapped to the products
class and the detail
method.
And then the ID will be set to 15
.
$routes['product/(:any)'] = "catalog/product_lookup";
An URL with product
as the first segment and any
thing in the second will be remapped to the catalog
class and the product_lookup
method.
$routes['product/(:num)'] = "catalog/product_lookup_by_id/$1";
An 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.
Reserved Routes
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.