Stephan Vierkant
Stephan Vierkant

Reputation: 10144

Multiple routes with multiple domains

Let's say I've got a website with multiple (sub)domains:

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

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?

Upvotes: 0

Views: 2816

Answers (2)

Maltronic
Maltronic

Reputation: 1802

You should use Symfony's hostname routing.

So your routes would look like:

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

Upvotes: 0

Michael Sivolobov
Michael Sivolobov

Reputation: 13265

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"

Then check in some listener before controller for valid url and process parameters.

Upvotes: 2

Related Questions