Patterns of MVC Frameworks: Router

A router’s job is to map incoming requests to specific application functionality.

The router will parse part of the URL used to make the request and see if it can find a match in it’s configuration. If a match isn’t found the user will get a 404, or be redirected. If the router does find a match it will process the request by passing it on to the designated controller and action. It may be possible to also attach a function directly to a route, rather than using controller files if this is preferred.

Here is what router code could look like:

Slim is a very small and simple microframework, it’s designed for smaller tasks. It’s router is configured with PHP and here we can see it matches part of a url from the HTTP GET request method.

The Symfony router is configured with YML here (although it can also be XML, PHP or annotations) and has a few extra options for matching a route.

The requirements options allow additional matching on other aspects such as matching on http host.

Default template allows us to define which view is displayed to the user. It is possible to define which HTTP methods are allowed with the Symfony router but by default it accepts them all.

Leave a Reply

Your email address will not be published. Required fields are marked *