Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conditional routing by multiple hosts #9373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tak-amboss opened this issue Oct 25, 2013 · 6 comments
Closed

Conditional routing by multiple hosts #9373

tak-amboss opened this issue Oct 25, 2013 · 6 comments

Comments

@tak-amboss
Copy link

This is related to #6848 and #5424. I am trying to use the host matching for changeing my routing resource by domain.

test:
    resource: "routing.yml"
    host:     "my.first.domain"

test2:
    resource: "routing.yml"
    host:     "any.other.domain"

This routing works well for "any.other.domain" but calling by "my.first.domain" I get a NotFoundHttpException. If I change the order of my entries to:

test2:
    resource: "routing.yml"
    host:     "any.other.domain"

test:
    resource: "routing.yml"
    host:     "my.first.domain"

it works well for "my.first.domain" but "any.other.domain" is broken now. Is there a way to get around this problem?

@sstok
Copy link
Contributor

sstok commented Oct 25, 2013

The host part is used both for matching and route generating (both by name), you can import an RouteCollection only once per host.

If you import the routes they inherit the configuration or there 'parent', if you import them again under a new host these will overwrite the ones previously registered (by there names).

If you really need to use multiple hostnames you can also change the host pattern to "^(my.\first.domain|any.other.domain)$"

@tak-amboss
Copy link
Author

Thanks for your reply. Unfortunately I cannot avoid importing the same RouteCollections for both domains, so I tried out the regex:

test:
    host:     "^(my.\first.domain|any.other.domain)$"
    resource: "routing.yml"

But like in #6848 this doesn't match any domain.

@Tobion
Copy link
Contributor

Tobion commented Oct 25, 2013

test:
    resource: "routing.yml"
    host:     "my.first.domain"
test2:
    resource: "routing.yml"
    host:     "any.other.domain"

The problem is that the routes are identified by name, so they are just overridden by the second import using host: "any.other.domain".

test:
    host:     "^(my.\first.domain|any.other.domain)$"
    resource: "routing.yml"

Sould be

test:
    host:     "{domain}.domain"
    resource: "routing.yml"
    requirements:
        domain: my\.first|any\.other

Please have a look at the routing documentation. Its the same style as for the path pattern.

@tak-amboss
Copy link
Author

Thanks for your suggestion, but using requirements would mean that I had to provide the domain parameter each time I generate a route like described in #6857. Using defaults wouldn't work for me as the user has to stay at the requested domain:

test:
    host:     "{domain}.domain"
    resource: "routing.yml"
    requirements:
        domain: my\.first|any\.other
    defaults:
        domain: my.first

With this config every generated route would point to my.first.domain even if the user requested any.other.domain.

@Tobion
Copy link
Contributor

Tobion commented Oct 25, 2013

Yes thats a known problem. See also #3965 that could solve it.
Closing as duplicate of #6857.

@Tobion Tobion closed this as completed Oct 25, 2013
@raziel057
Copy link
Contributor

@tak-amboss As a workarround, as host don't support pattern you can use a condition:

test:
    condition: "request.headers.get('Host') matches '^(my.\first.domain|any.other.domain)$'"
    resource: "routing.yml"

But it's strange for me that pattern is supported by host declaration in firewall but not in routing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants