使用主机路由到Symfony中的子域时,如何忽略顶级域



我有一个网站,我正在使用 domain.lc 在本地进行测试。该网站有一个子域 sub.domain.lc,该子域路由到其他控制器,如下所示:

my_bundle:
    host:     sub.domain.lc
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /

由于主机,子域路由到我的捆绑包,但我希望它也使用 sub.domain.com 路由到此捆绑包。有没有办法忽略使用主机的顶级域?

你可以像这样使用占位符:

my_bundle:
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
    host:     "{domain}"
    defaults:
        domain: "%domain%"
    requirements:
        domain: "sub.domain.(lc|com)"

但是生成绝对 URL 存在问题。这取决于您的应用程序当前运行的位置(loc 或 com),您需要将其指定为容器参数(parameters.yml 是好地方)。链接到类似的问题。

尽管 kba 确实提供了一个对我有很大帮助的答案,而且我实际上会接受他的答案是正确的,但我想向您展示我的路线最终是什么样子的:

my_bundle:
    host:     sub.domain.{tld}
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
    requirements:
        tld: lc|nl
    defaults:
        tld: nl

我没有在路由中使用服务容器参数,因为我知道我的域不会更改。只有我的顶级域名可以更改。

最新更新