具有多个域的多个路由 [英] Multiple routes with multiple domains
问题描述
假设我有一个拥有多个(子)域的网站:
Let's say I've got a website with multiple (sub)domains:
- acme.com(美国)
- acme.nl(荷兰)
- be.acme.eu(比利时)
- de.acme.eu(德国)
- fr.acme.eu(法国)
- 等...
- acme.com (USA)
- acme.nl (Netherlands)
- be.acme.eu (Belgium)
- de.acme.eu (Germany)
- fr.acme.eu (France)
- etc...
我认为这应该配置起来非常简单,所以我做了这个routing.yml:
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
但是如果我运行 router:debug
最后一条路线(在这种情况下为 {country} .acme.eu
) 出现。如果我更改顺序,则会显示最后一个选项。
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?
推荐答案
这是因为所有路由都指向一个资源。
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"
然后在控制器之前检入某些侦听器,以获取有效的URL和进程参数。
Then check in some listener before controller for valid url and process parameters.
这篇关于具有多个域的多个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!