具有多个域的多个路由

Multiple routes with multiple domains

假设我有一个包含多个(子)域的网站:

我认为这应该很容易配置,所以我做了这个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)会显示。如果我更改顺序,最后一个选项会出现。

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

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

但你可以使用另一种解决方案:

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

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

你应该使用 Symfony 的 hostname routing.

所以你的路线看起来像:

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