Search code examples
phpsymfonysymfony4

Symfony 4.1 i18n route prefix: exclude route


I have a i18n project running on Symfony 4.1.

I want to achieve some routing like:

  • / => a "Choose language" page route
  • /fr/ or /en/ => the homepage route
  • /fr/foo/, /en/bar, etc => the other routes

Following this article, I'm using annotations to handle routing and I set a global prefix to my annotations routing :

site:
    resource: '../src/Controller/'
    type: annotation
    prefix:
        en: '/en'
        fr: '/fr'

This way, all the routes defined in annotations from '../src/Controller/' are prefixed, which leads to a conflict between my "Choose language" page and the homepage.

I want to exclude the "Choose language" page from the i18n prefixing, so I'll be able to use "/" for this page, when the homepage could stay on /fr or /en.

Any ideas to achieve this?


Solution

  • Defining this route at top of annotations.yaml without prefix option:

    _choose_language:
        path: /
        controller: App\Controller\ChooseLanguageController
    
    site:
        resource: '../src/Controller/'
        type: annotation
        prefix:
            en: '/en'
            fr: '/fr'
    

    and don't use route annotation for this special controller:

    class ChooseLanguageController
    {
        public function __invoke()
        {
            return new Response('Choose Language!');
        }
    }
    

    then this action is being excluded from i18n prefixing.