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/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.

For example, let’s say you want your URLs to have this prototype:

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 method name, but in the example above it instead has a product ID. To overcome this, Codingox allows you to remap the URI handler.

Setting your own routing rules

Routing rules are defined in your config/router.php file. In it you’ll see an array called $router that permits you to specify your own routing criteria.

Examples

Here are a few routing examples:

$router['journals'] = 'blogs';

A URL containing the word “journals” in the first segment will be remapped to the “blogs” class.

$router['blog/joe'] = 'blogs/users';

A URL containing the segments blog/joe will be remapped to the “blogs” class and the “users” method.

Reserved Routes

There are few reserved routes:

$router['default'] = 'welcome';

This route points to the action that should be executed if the URI contains no data, which will be the case when people load your root URL. The setting accepts a controller/method value and index() would be the default method if you don’t specify one. In the above example, it is Welcome::index() that would be called.

Note

You can NOT use a directory as a part of this setting!