Contributed by
Arnaud Le Blanc
in #3378.

To celebrate the first release candidate of Symfony 2.2.0, I want to talk about one of its greatest new features: support for the URL host in routes. That was probably one of the most often requested feature for Symfony, and 2.2 finally has native support for it. Let me quickly show you how it works.

When a request comes in, the Symfony Routing component tries to find a matching Route. The matching process is mainly tied to the request path (/blog/what-a-wonderful-day), but it also takes into account other Request parameters like the HTTP method (GET, POST, ...) or the HTTP scheme (http or https). As of 2.2, the host becomes one more factor used by the Routing to select the matching route (fabien.example.com).

The host constraint works in the exact same way as the pattern one: it can contain placeholders, the placeholders can have requirements, and a placeholder requirement can use some service container parameter to be configured.

Here is a typical usage example:

1
2
3
4
5
6
7
8
user_homepage:
    path: /
    host: "{user}.example.com"
    defaults: { _controller: AcmeDemoBundle:User:profile }

main_homepage:
    path:  /
    defaults: { _controller: AcmeDemoBundle:Main:homepage }

The host constraint can also be added to a set of routes when importing them.

Want to learn more about this feature? ... there is a cookbook entry for that!