Olivier Dolbeau
Contributed by Olivier Dolbeau in #36187

Symfony 4.1 added internationalized routing to allow defining different URLs per locale. In Symfony 5.1 we've improved this feature to also allow different hosts per locale.

In practice, this change means that you can now define an array of values for the host option. This works both in routes config and when importing groups of routes. For example, this would define different hosts for English and French locales in all routes defined as controller annotations:

1
2
3
4
5
6
7
# config/routes/annotations.yaml
controllers:
    resource: '../../src/Controller/'
    type: annotation
    host:
        fr: www.example.fr
        en: www.example.com

Individual routes can define their own localized host too (and it overrides any other host config set while importing the routes). Lastly, something to keep in mind is that when using localized hosts, if a route uses a locale without a host defined for it, you'll see an exception (there is no default host value).

Published in #Living on the edge