Symfony routing - locale as subdomain with default return

I am trying to configure a routing system with a subdomain representing the current language. Routing is set using the @Routing annotation and looks like this:

 /** * @Route( * "/", * name="homepage", * host="{locale}.{domain}", * defaults={"locale" = "en", "domain" = "%domain%"}, * requirements={"locale" = "en|de|fr", "domain" = "%domain%"} * ) */ 

Works well for URLs like en.somedomain.com or de.somedomain.com , but cannot find the correct route for somedomain.com , without the locale.

I understand that because of the host parameter, which is set to represent the exact locale.domain template, but I can’t find a way to tell the Symfony routing system that there may be additional default host .

I searched all around, but did not find anything special. Would thank for any help!

UPDATE

There is actually a way to do this by adding another @Route to the annotation, without the host parameter:

 /** * @Route( * "/", * name="homepage_default", * defaults={"locale" = "en"} * ) */ 

but it looks a little dirty, and I don’t use the %domain% parameter there, which is important for me - say, if I need another subdomain for the mobile version.

+6
source share
2 answers

Well, it seems that triple annotation routing for locale + subdomain processing is the only choice at the moment.

Studying the documentation (for example, in this article ) shows that Symfony developers encourage us to do this in a way that is not pleasant for me. However, here is the solution ...

 /** * @Method({"GET"}) * @Route( * "/", * name="homepage", * host="{mobile}.{_locale}.{domain}", * defaults={"mobile" = "moblie", "locale" = "%locale%", "domain" = "%domain%"}, * requirements={"mobile" = "mobile|m", "_locale" = "%locale%|de|fr", "domain" = "%domain%"} * ) * @Route( * "/", * name="homepage", * host="{_locale}.{domain}", * defaults={"_locale" = "%locale%", "domain" = "%domain%"}, * requirements={"_locale" = "%locale%|de|fr", "domain" = "%domain%"} * ) * @Route( * "/", * name="homepage_default", * defaults={"_locale" = "%locale%"} * ) */ 

Note that order is important, starting with subdomains and ending with the default value. Since this looks ugly with the @Route annotation, I decided to rewrite it also in YAML :

 homepage_locale_mobile: path: / host: "{mobile}.{_locale}.{domain}" defaults: { mobile: "mobile", _locale: "%locale%", domain: "%domain%" } requirements: mobile: "mobile|m" _locale: "%locale%|de|fr", domain: "%domain%" homepage_locale: path: / host: "{_locale}.{domain}" defaults: { _locale: "%locale%", domain: "%domain%" } requirements: _locale: "%locale%|de|fr", domain: "%domain%" homepage: path: / defaults: { _locale: "%locale%" } 

Ordered as well. Maybe someone will come across and use it.

+5
source

I had a similar problem with the default settings, although it was a route parameter, not a locale.

The solution was to replace the = sign as follows: as it should be, and the compiler somehow does not complain about it.

So try this:

 /** * @Route( * "/", * name="homepage", * host="{locale}.{domain}", * defaults={"locale" : "en", "domain" : "%domain%"}, * requirements={"locale" : "en|de|fr", "domain" : "%domain%"} * ) */ 
0
source

Source: https://habr.com/ru/post/980634/


All Articles