Symfony2 – playing with default locale

By | 14/10/2014

The Symfony2 documentation is quit good, but this morning I missed a explanation for using a default locale without url parameter together with routing and a routing configuration in annotations.

What I wanted to do is the following url setup:

http://www.domain.com -> default locale should be used without any prefix
http://www.domain.com/en -> english translation should be used with prefix

I also wanted to do the routing with annotations inside the controllers.

Following setup seems to work:

The important part here is to start with the | when setting the requirements and to leave away the de that you might want to set.

Of course you have to set the default_locale in your configuration.

Your annotations inside your controller look like the following then:

It took me some time to figure out how this setup works. Perhaps it helps someone.

If you also want to have the “de” in your url, you have to add the “de” after the empty pipe, but then you have the problem that you can reach the german version with and without the “de” what means you will have duplicate content.