Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale in Routing, Default Language also without Parameter

I want to define the page language via url on a Symfony2 installation. My routing works via annotation inside the controller.

routing.yml

_index:
    resource: "@MyMainBundle/Controller/SiteController.php"
    type:     annotation

siteController.php

/**
 * @Route( "/{_locale}/{site}", name="_site_site", defaults={"_locale" = "en"}, requirements={"site" = "about|cities|download|blog", "_locale" = "en|de|fr|es"} )
 */

This works quiet well, but waht I want, is that the following url call the same action.

http://example.com/download
http://example.com/en/download
http://example.com/de/download

Without the languge-parameter, the page should switch back to the default language, but this is something I can handle inside my action.

I found this Answer, but could not get it to work at all. Symfony2 default locale in routing

like image 997
madc Avatar asked Dec 12 '22 06:12

madc


2 Answers

Just add another @Route annotation that does not include the locale.

/**
 * @Route("/{_locale}/{site}/")
 * @Route("/{site}/")
 */
like image 122
Kris Wallsmith Avatar answered Dec 17 '22 06:12

Kris Wallsmith


This also works within annotations.yaml

frontend_controllers:
    resource: ../../src/Controller/Frontend
    type: annotation
    prefix:
        - /
        - /{_locale}
    defaults:
        _locale: 'en'
like image 30
Tobias Bauer Avatar answered Dec 17 '22 05:12

Tobias Bauer