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

在Symfony中使用主机路由到子域时,如何忽略顶级域?,symfony,routing,tld,Symfony,Routing,Tld,我有一个网站,我正在使用domain.lc进行本地测试。该网站有一个子域名sub.domain.lc,路由到不同的控制器,如下所示: my_bundle: host: sub.domain.lc resource: "@MyBundle/Controller/" type: annotation prefix: / my_bundle: resource: "@MyBundle/Controller/" type: a

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

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

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

您可以这样使用占位符:

my_bundle:
    host:     sub.domain.lc
    resource: "@MyBundle/Controller/"
    type:     annotation
    prefix:   /
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
我没有在路由中使用服务容器参数,因为我知道我的域不会更改。只有我的顶级域名可以更改