Search code examples
internationalizationroutessymfonylocale

Prefixing routes with locale in Symfony2


Here is my setup, a bit changed standard edition.

# app/config/routing.yml
_internal:
    resource: "@AcmeDemoBundle/Controller/"
    prefix: /{_locale}
    requirements:
        _locale: en|sr

And added /{_locale} in security.yml.

Routes before:

Name                      Method Pattern
_welcome                  ANY    /
_demo_login               ANY    /demo/secured/login
_security_check           ANY    /demo/secured/login_check
_demo_logout              ANY    /demo/secured/logout
acme_demo_secured_hello   ANY    /demo/secured/hello
_demo_secured_hello       ANY    /demo/secured/hello/{name}
_demo_secured_hello_admin ANY    /demo/secured/hello/admin/{name}
_demo                     ANY    /demo/
_demo_hello               ANY    /demo/hello/{name}
_demo_contact             ANY    /demo/contact
_wdt                      ANY    /_wdt/{token}
_profiler_search          ANY    /_profiler/search
_profiler_purge           ANY    /_profiler/purge
_profiler_import          ANY    /_profiler/import
_profiler_export          ANY    /_profiler/export/{token}.txt
_profiler_search_results  ANY    /_profiler/{token}/search/results
_profiler                 ANY    /_profiler/{token}
_configurator_home        ANY    /_configurator/
_configurator_step        ANY    /_configurator/step/{index}
_configurator_final       ANY    /_configurator/final

Routes after:

Name                      Method Pattern
_welcome                  ANY    /
_wdt                      ANY    /_wdt/{token}
_profiler_search          ANY    /_profiler/search
_profiler_purge           ANY    /_profiler/purge
_profiler_import          ANY    /_profiler/import
_profiler_export          ANY    /_profiler/export/{token}.txt
_profiler_search_results  ANY    /_profiler/{token}/search/results
_profiler                 ANY    /_profiler/{token}
_configurator_home        ANY    /_configurator/
_configurator_step        ANY    /_configurator/step/{index}
_configurator_final       ANY    /_configurator/final
_demo_login               ANY    /{_locale}/demo/secured/login
_security_check           ANY    /{_locale}/demo/secured/login_check
_demo_logout              ANY    /{_locale}/demo/secured/logout
acme_demo_secured_hello   ANY    /{_locale}/demo/secured/hello
_demo_secured_hello       ANY    /{_locale}/demo/secured/hello/{name}
_demo_secured_hello_admin ANY    /{_locale}/demo/secured/hello/admin/{name}
_demo                     ANY    /{_locale}/
_demo_hello               ANY    /{_locale}/hello/{name}
_demo_contact             ANY    /{_locale}/contact

Which ofc leads to No route found for "GET /en/demo/hello/World" for /en/demo/hello/World url.

Any idea how to fix this?

Additionally, I would like to implement per domain locale. I was trying https://github.com/schmittjoh/JMSI18nRoutingBundle, but for some reason it does not work with latest Symfony 2.0.9, at least for me. I was following the doc, but I got similar 404 error when trying prefixed urls.


Solution

  • Defining default values and requirements for placeholders in prefix is not possible in Symfony version 2.0.9. You'd have to upgrade to 2.1, which is currently in active development.