Archives
Master Symfony2 fundamentals
Symfony hosting done right
Discover the SensioLabs Support
Blog
New in Symfony 2.2: URL host support in the Routing
by
Fabien Potencier
– February 12, 2013
– 38 comments
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!





is a trademark of Fabien Potencier. All rights reserved.
Add a Comment
Comments
However I can't make it work for an app with 2 bundles hosted on 2 subdomains:
AcmeMyTestBundle:
resource: "@AcmeMyTestBundle/Resources/config/routing.yml"
prefix: /
host: mytest.{domain}
requirements:
domain: %domain%
(same code for AcmeMyTest2Bundle with a mytest2.{domain} host).
I defined the domain value in parameters.yml (its value differs for each environment - e.g. localhost.com for dev environment).
But I get the "Some mandatory parameters are missing ("domain")" error... would you have any idea why?
user_homepage:
path: /
host: "{user}.example.com"
defaults: { _controller: AcmeDemoBundle:User:profile }
user_homepage:
path: /
host: "{user}.example.com"
defaults: { _controller: AcmeDemoBundle:User:profile }
host: "{user}.example.com"
host: ''{user}.example.com''
What's up with all these multiposts ?
- { host: admin.example.net, path: ^/, role: ROLE_ADMIN }
http://symfony.com/doc/master/book/security.html#understanding-how-access-control-works