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 app/config/. You can set this routing applied into production, staging, or development depend on your requirements. On each configuration files, Seme Framework has created the commented section for rewriting URI in the routing section. 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.

Note

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

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 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";

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.

Important

Do not use leading/trailing slashes in routes key or value.


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.

Important

The reserved routes must come before any wildcard routes.

Another reserved route is admin secret route. Please refer to this docs for using admin secret routing.