Multiple routes with multiple domains

允我心安 提交于 2019-12-12 06:47:31

问题


Let's say I've got a website with multiple (sub)domains:

  • acme.com (USA)
  • acme.nl (Netherlands)
  • be.acme.eu (Belgium)
  • de.acme.eu (Germany)
  • fr.acme.eu (France)
  • etc...

I thought this should be very simple to configure, so I made this routing.yml:

usa:
    host: "acme.com"
    resource: "@WebsiteBundle/Controller/"
    type:     annotation
    defaults:
        country: "en"

netherlands:
    host: "acme.nl"
    resource: "@WebsiteBundle/Controller/"
    type:     annotation
    defaults:
        country: "nl"

europe:
    host: "{country}.acme.eu"
    resource: "@WebsiteBundle/Controller/"
    type:     annotation

But if I run router:debug, only the last route (in this case {country}.acme.eu) shows up. If I change to order, the last option shows up.

How can I use different (sub)domains for all my countries?


回答1:


This is because all the routes point to one resource. Every later route will override routes defined before.

But you can use another solution:

main_route:
    host: "{country}.acme.{domain}"
    resource: "@WebsiteBundle/Controller/"
    type:     annotation
    defaults:
        country: "en"

Then check in some listener before controller for valid url and process parameters.




回答2:


You should use Symfony's hostname routing.

So your routes would look like:

international_homepages:
    path:     /
    host:     "{subdomain}.acme.{extension}"
    defaults:
        _controller: AcmeBundle:Main:defaultHomepage
        subdomain: www
        extension: com


来源:https://stackoverflow.com/questions/30705413/multiple-routes-with-multiple-domains

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!