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

Skip to content

[Routing] Routes with host/locale matrix not working correctly #38209

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
Wirone opened this issue Sep 16, 2020 · 9 comments
Closed

[Routing] Routes with host/locale matrix not working correctly #38209

Wirone opened this issue Sep 16, 2020 · 9 comments

Comments

@Wirone
Copy link
Contributor

Wirone commented Sep 16, 2020

Symfony version(s) affected: 5.1.5

Description
I've followed #30617 (and symfony/symfony-docs#13571) for configuring support for locales based on host. When I access page through host mapped to non-default locale, page still renders in default locale.

How to reproduce

# config/routes.yaml
foo:
    resource: routes/foo.yaml
    prefix: /
    defaults:
        _format: html
    host:
        en: 'foo.loc'
        pl: 'pl.foo.loc'
# config/routes/foo.yaml
app.foo.landing_page:
    path: /
    methods: [GET]
    controller: App\Foo\LandingPageAction

With config above RedirectableCompiledUrlMatcher has this route collection for this path:

# var/cache/dev/url_matching_routes.php
'/' => [
    [['_route' => 'app.foo.landing_page', '_controller' => 'App\\Foo\\LandingPageAction'], null, ['GET' => 0], null, false, false, null],
    [['_route' => 'app.foo.landing_page', '_controller' => 'App\\Foo\\LandingPageAction', '_locale' => 'en', '_format' => 'html'], 'foo.loc', ['GET' => 0], null, false, false, null],
    [['_route' => 'app.foo.landing_page', '_controller' => 'App\\Foo\\LandingPageAction', '_locale' => 'pl', '_format' => 'html'], 'pl.foo.loc', ['GET' => 0], null, false, false, null],
],

Unfortunately, the first one is matched when I access page via pl.foo.loc, so locale is not set and page displays in default locale.

ℹ️ When I comment out first rule, matching hosts work and locale is set properly.

Maybe I do something wrong but mentioned docs are not merged so support for this feature is somehow undocumented 😉

Possible Solution
Move non-strict rule (without host matching) to the end of the collection for path, so it can run as fallback if other rules (hosts) aren't matched.


CC: @odolbeau

@odolbeau
Copy link
Contributor

Given generated matching routes, it looks like the problem may be related to the _format parameter.
Could you please try to remove this from your config/routes.yaml file to confirm @Wirone?

@Wirone
Copy link
Contributor Author

Wirone commented Sep 16, 2020

@odolbeau Thanks, additional info:

Without defaults:

'/' => [
    [['_route' => 'app.foo.landing_page', '_controller' => 'App\\Foo\\LandingPageAction'], null, ['GET' => 0], null, false, false, null],
    [['_route' => 'app.foo.landing_page', '_controller' => 'App\\Foo\\LandingPageAction', '_locale' => 'pl'], 'pl.foo.loc', ['GET' => 0], null, false, false, null],
],

With _format: json in defaults:

'/' => [
    [['_route' => 'app.foo.landing_page', '_controller' => 'App\\Foo\\LandingPageAction'], null, ['GET' => 0], null, false, false, null],
    [['_route' => 'app.foo.landing_page', '_controller' => 'App\\Foo\\LandingPageAction', '_locale' => 'pl', '_format' => 'json'], 'pl.foo.loc', ['GET' => 0], null, false, false, null],
],

So that non-strict rule is always there as a first one.

FYI: I've run cache:clear after every change to be sure it's fresh. And removed en: 'foo.loc' from config so output is smaller.

@stof
Copy link
Member

stof commented Sep 16, 2020

@nicolas-grekas is it expected that we still have a route without any host requirement when we use locale-based host requirements ?

@Wirone
Copy link
Contributor Author

Wirone commented Sep 30, 2020

@xabbuh could you remove "Waiting feedback" label, since I've provided additional information 2 weeks ago?

@nicolas-grekas could you take a look at @stof's comment?

@nicolas-grekas
Copy link
Member

is it expected that we still have a route without any host requirement when we use locale-based host requirements ?

Nope, it's not. PR welcome.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@Wirone
Copy link
Contributor Author

Wirone commented Apr 7, 2021

Bug still exists in Symfony 5.2.6, here's reproducer → https://github.com/Wirone/symfony-issue-38209

@carsonbot carsonbot removed the Stalled label Apr 7, 2021
@Wirone
Copy link
Contributor Author

Wirone commented Aug 30, 2021

Bug still exists on 5.3, I've pushed commit with bumped dependencies to reproducer.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jan 18, 2022

It took me a while to figure this out, but there is no bug here:
the route is imported twice:

  • once with host info thanks to your import statement in routes.yaml
  • twice without host info because of your Kernel's $routes->import('../config/{routes}/*.yaml');

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

No branches or pull requests

6 participants