Skip to content
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

Locale in path #92

Open
aistis- opened this issue Sep 26, 2016 · 3 comments
Open

Locale in path #92

aistis- opened this issue Sep 26, 2016 · 3 comments

Comments

@aistis-
Copy link

aistis- commented Sep 26, 2016

Hello,

I have a case where the paths of the site looks:

  • example.com/en/banana
  • example.com/fr/banane
  • example.com/lt/bananas

My current routing definition:

# app/config/routing.yml
my_fruits:
    resource: '@MyFruitsBundle/Resources/config/routing/banana.yml'
    type: be_simple_i18n
    prefix:   /{_locale}/
    requirements:
        _locale: 'en|fr|lt'
# src/My/FruitsBundle/Resources/config/routing/banana.yml
my_fruits_banana:
    locales:
        en: '/banana'
        fr: '/banane'
        lt: '/bananas'
    defaults:
        _controller: 'MyFruitsBundle:Banana:list'

How would be possible to deny paths with mismatched _locale and parameter with a translation locale?

What I want to achieve:

  • example.com/en/banana - 200
  • example.com/fr/banane - 200
  • example.com/lt/bananas - 200
  • example.com/fr/banana - 404
  • example.com/lt/banane - 404
  • example.com/en/bananas - 404
@aistis-
Copy link
Author

aistis- commented Sep 28, 2016

Actually this configuration works! BUT it does not work, if between app/config/routing.yml and src/My/FruitsBundle/Resources/config/routing/banana.yml exist middleware routing file with no prefix defined. I18nGenerator is not able to generate prefixed paths anymore.

@dev-francois
Copy link

Hey @aistis-
I try to achieve exaclty that. I can't make it work though.
Are you saying it works? I tried to add a prefix in app/config/routing.yml, but example.com/fr/banana still send back a 200.

@frankdejonge
Copy link

frankdejonge commented Mar 16, 2017

@aistis- you can import a route file with a prefix like:

my_fruits:
    resource: '@MyFruitsBundle/Resources/config/routing/banana.yml'
    type: be_simple_i18n
    prefix:
        en: en
        fr: fr

This is not only strict, it's also WAY more performant in terms of route matching. Because the dumped Symfony routes does static prefix optimisations in order to prevent regex matches, with the way you do it every route check is a regex. For more tips on this, I blogged about it: https://blog.frankdejonge.nl/symfony-routing-performance-considerations/

Note that the route definition itself needs to be imported as type: be_simple_i18n.

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

4 participants