Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding attributes to the path request with twig in symfony2

I've been googleing this question but I can't find anyone with my same problem... And I don't think I'm the only one here >.<

Let's see, I'm using translations in symfony2. I NEED to use twig for this... The thing is that I need 3 links so people can change the site's language. The link has to redirect to the same page the user is, but changing the '_locale'.

I first thought in something like this:

// in routing.yml
bundleStuff_someUrl:
    pattern:  /{_locale}/aloha
    defaults: { _controller: bundleStuff:Aloha:foo }

bundleStuff_fooUrl:
    pattern:  /{_locale}/foo/{fooParam}
    defaults: { _controller: bundleStuff:Foo:foo }

// in view.html.twig
<a href="{{ path((app.request.get('_route'), { '_locale': 'l1' }) }}">lang1</a>
<a href="{{ path((app.request.get('_route'), { '_locale': 'l2' }) }}">lang2</a>
<a href="{{ path((app.request.get('_route'), { '_locale': 'l3' }) }}">lang3</a>

The problem becomes when (in this case) the _route is fooUrl... Is there a way to append every attribute I have in the current view to the path I'm looking for? In other words referring to this example: is there a way so twig knows it has to add the 'fooParam' to the path if the current view is 'fooUrl'?

Thank's in advance! Hope this post is useful! :D

like image 941
ThisIsErico Avatar asked Sep 12 '12 20:09

ThisIsErico


1 Answers

_route_params request attribute holds the parameters of the current route. So the twig code would be,

{% set route = app.request.get('_route') %}
{% set route_params = app.request.get('_route_params') %}

<a href="{{ path(route, route_params | merge({ '_locale': 'l1' })) }}">lang1</a>
<a href="{{ path(route, route_params | merge({ '_locale': 'l2' })) }}">lang2</a>
<a href="{{ path(route, route_params | merge({ '_locale': 'l3' })) }}">lang3</a>
like image 185
Mun Mun Das Avatar answered Sep 27 '22 01:09

Mun Mun Das