SymfonyWorld Online 2021 Winter Edition December 9 – 10, 2021 100% Online 25 talks and 10 workshops

New in Symfony 5.1: Different hosts per locale

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

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).

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

Nice addition, thank you :)
Great feature! Thanks Olivier
Wow! Nice!
Great, now I can move to symfony5! Thanks!
Yyy ! nice to have this natively, struggled simulating this feature in sf 3 and 4
\o/
super:)
What about router.request_context.host in parameter? Is still working?
What about url in command line?

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.