具有多个域的多个路由

时间:2015-06-08 09:23:05

标签: symfony routing

我们说我有一个有多个(子)域名的网站:

  • acme.com(美国)
  • acme.nl(荷兰)
  • be.acme.eu(比利时)
  • de.acme.eu(德国)
  • fr.acme.eu(法国)
  • 等...

我认为这应该非常简单,所以我做了这个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

但如果我运行router:debug,则只显示最后一条路线(在本例中为{country}.acme.eu)。如果我更改为订单,则会显示最后一个选项。

如何为所有国家/地区使用不同的(子)域名?

2 个答案:

答案 0 :(得分:1)

这是因为所有路由都指向一个资源。以后的每条路线都会覆盖之前定义的路线。

但您可以使用其他解决方案:

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

然后在控制器之前检查一些监听器以获取有效的url和进程参数。

答案 1 :(得分:0)

您应该使用Symfony' hostname routing

所以你的路线看起来像:

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