New in Symfony 2.2: URL host support in the Routing

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!

Comments

This is awesome!
This is what I was looking for. Awesome!
In this example the option is "hostname_pattern" but in the cookbook it's "host". Which one is correct?
Nevermind, by the time of writing my previous comment, it has been fixed ^^
Wooohooo!
This is awesome indeed, but we often need to connect a hostname to a language, for example: www.dutchdomainname.be must use 'nl' as '_locale' while www.frenchdomainname.be must use 'fr' as '_locale'. When generating the path also the correct domainname must be in the result link. I don't think this is possible yet. We now create a custom router for the url generation and uses the lunetics/LocaleBundle for the locale detection based on hostname.
Awesome feature indeed!

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?
Concerning my last comment... I didn't notice the exception is thrown during the rendering of templates with a call to Twig path(). I think I can get this sorted out now.
Hell Yeah! Time to get rid of rubbish workarouds!
Cool feature indeed
Great new feature !
This feature is indeed very usefull.
Nice feature!
Seems very handy!
Good news :) Thx a lot!
what about firewalls? how can i set a host for a seacure-area ?
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
The user example will not work since YAML will try to parse it as a mapping. You should enquote the host if it starts with a placeholder:

user_homepage:
path: /
host: "{user}.example.com"
defaults: { _controller: AcmeDemoBundle:User:profile }
The above example will not work, since YAML will see the user placeholder and will try to parse it as a mapping. When you start the host constraint with a placeholder you should enquote it.

user_homepage:
path: /
host: "{user}.example.com"
defaults: { _controller: AcmeDemoBundle:User:profile }
The above example will not work, since YAML will see the user placeholder and will try to parse it as a mapping. When you start the host constraint with a placeholder you should enquote it.
host: "{user}.example.com"
The above example will not work, since YAML will see the user placeholder and will try to parse it as a mapping. When you start the host constraint with a placeholder you should enquote it.
host: ''{user}.example.com''
@volker Actually, for what you need, you don't need a host based firewall but a very simple one with access control (which support the "host" parameter).

What's up with all these multiposts ?
For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
Great addition!
@Soyel I got a 500 server error after submitting my reply, so I thought it didn't sumbit and I retried it a couple of times. I then looked and to my surpries the posts were all there, I couldn't however remove them.
I've just fixed the example as the quotes are indeed required for the path.
The 500 error problem has just been fixed by the way.
Awesome! Just need this feature for my new 2.1 symfony site. When do you think that version 2.2 will be ready? And will be diffucult migrate from 2.1? Thanks!
Great! Will the security control module be aware of this? I mean the access_control sections, e.g:

- { host: admin.example.net, path: ^/, role: ROLE_ADMIN }
@xis: yes it does.

http://symfony.com/doc/master/book/security.html#understanding-how-access-control-works
that is a very nice feature. thank you
And how about few domains binded to service? I got a .example.com .example.net .examplenet.com, how to pass all of this domains?

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.