Filling in all required parameters

Symfony2 has a multilingual website. The base layout has a language switch something like this:

<a href="{{ path(app.request.attributes.get('_route'), {_locale: 'en'}) }}">EN</a> <a href="{{ path(app.request.attributes.get('_route'), {_locale: 'fr'}) }}">FR</a> 

This is great for switching languages ​​without changing the current page. However, if there are other parameters, it throws an exception due to the "lack of required parameters." How to overcome this?

+4
source share
2 answers

You can do something like:

 <a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'_locale': 'en'})) }}">EN</a> <a href="{{ path(app.request.attributes.get('_route'), app.request.query.all|merge({'_locale': 'fr'})) }}">FR</a> 

What this means is merging the _locale parameter with existing query parameters.

+4
source

This is my solution, working with Symfony 2.2 to 2.5

 <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'en'})) }}">English</a> <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'fr'})) }}">Français</a> <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'es'})) }}">Español</a> 

This is my solution, working with Symfony 2.0 to 2.1

 <a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale': 'en'})) }}">English</a> <a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale': 'fr'})) }}">Français</a> <a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale': 'es'})) }}">Español</a> 
+2
source

All Articles