Archives


Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

Fabien Potencier
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!

Comments RSS

  • Daniel Londero
    #1 Daniel Londero said on the 2013/02/12 at 09:32
    This is awesome!
  • Farhad Safarov
    #2 Farhad Safarov said on the 2013/02/12 at 09:35
    This is what I was looking for. Awesome!
  • Hugo Hamon
    #3 Hugo Hamon said on the 2013/02/12 at 09:38
    In this example the option is "hostname_pattern" but in the cookbook it's "host". Which one is correct?
  • Hugo Hamon
    #4 Hugo Hamon said on the 2013/02/12 at 09:39
    Nevermind, by the time of writing my previous comment, it has been fixed ^^
  • Grayson Koonce
    #5 Grayson Koonce said on the 2013/02/12 at 09:44
    Wooohooo!
  • Kris Pypen
    #6 Kris Pypen said on the 2013/02/12 at 10:07
    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.
  • Michaël Perrin
    #7 Michaël Perrin said on the 2013/02/12 at 11:00
    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?
  • Michaël Perrin
    #8 Michaël Perrin said on the 2013/02/12 at 11:17
    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.
  • Max Małecki
    #9 Max Małecki said on the 2013/02/12 at 11:18
    Hell Yeah! Time to get rid of rubbish workarouds!
  • Driss Najam
    #10 Driss Najam said on the 2013/02/12 at 12:17
    Cool feature indeed
  • Pierre-Charles Bertineau
    #11 Pierre-Charles Bertineau said on the 2013/02/12 at 14:04
    Great new feature !
  • Peter Kokot
    #12 Peter Kokot said on the 2013/02/12 at 14:37
    This feature is indeed very usefull.
  • Cyrille Bourgois
    #13 Cyrille Bourgois said on the 2013/02/12 at 14:43
    Nice feature!
  • Michael Klik
    #14 Michael Klik said on the 2013/02/13 at 07:03
    Seems very handy!
  • Dmitry Abramov
    #15 Dmitry Abramov said on the 2013/02/13 at 11:32
    Good news :) Thx a lot!
  • Volker
    #16 Volker said on the 2013/02/13 at 16:59
    what about firewalls? how can i set a host for a seacure-area ?
  • Tal Ater
    #17 Tal Ater said on the 2013/02/13 at 21:22
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Tal Ater
    #18 Tal Ater said on the 2013/02/13 at 21:22
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Tal Ater
    #19 Tal Ater said on the 2013/02/13 at 21:22
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Tal Ater
    #20 Tal Ater said on the 2013/02/13 at 21:22
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Tal Ater
    #21 Tal Ater said on the 2013/02/13 at 21:22
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Tal Ater
    #22 Tal Ater said on the 2013/02/13 at 23:25
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Tal Ater
    #23 Tal Ater said on the 2013/02/13 at 23:26
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Luuk Scholten
    #24 Luuk Scholten said on the 2013/02/14 at 01:09
    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 }
  • Luuk Scholten
    #25 Luuk Scholten said on the 2013/02/14 at 01:10
    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 }
  • Luuk Scholten
    #26 Luuk Scholten said on the 2013/02/14 at 01:10
    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"
  • Luuk Scholten
    #27 Luuk Scholten said on the 2013/02/14 at 01:10
    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''
  • Soyel
    #28 Soyel said on the 2013/02/14 at 07:56
    @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 ?
  • Tal Ater
    #29 Tal Ater said on the 2013/02/14 at 08:56
    For those still using Symfony 1.x, domain (host) routes are supported via sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin)
  • Casey Klebba
    #30 Casey Klebba said on the 2013/02/14 at 09:05
    Great addition!
  • Luuk Scholten
    #31 Luuk Scholten said on the 2013/02/14 at 10:14
    @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.
  • Fabien Potencier
    #32 Fabien Potencier said on the 2013/02/14 at 10:24
    I've just fixed the example as the quotes are indeed required for the path.
  • Fabien Potencier
    #33 Fabien Potencier said on the 2013/02/14 at 10:25
    The 500 error problem has just been fixed by the way.
  • Jordi Casadevall
    #34 Jordi Casadevall said on the 2013/02/14 at 19:53
    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!
  • xis
    #35 xis said on the 2013/02/16 at 10:41
    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 }
  • Soyel
    #36 Soyel said on the 2013/02/20 at 05:36
    @xis: yes it does.

    http://symfony.com/doc/master/book/security.html#understanding-how-access-control-works
  • Laurynas Mališauskas
    #37 Laurynas Mališauskas said on the 2013/02/21 at 18:36
    that is a very nice feature. thank you
  • Seweryn Zeman
    #38 Seweryn Zeman said on the 2013/04/06 at 15:20
    And how about few domains binded to service? I got a .example.com .example.net .examplenet.com, how to pass all of this domains?