New in Symfony 2.2: URL host support in the Routing
February 12, 2013 • Published by Fabien Potencier
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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!
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
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