18
votes

I have an url like : MYURL?filter[_per_page]=25&filter[name][value]=hello

How can i get these parameters with twig ?

I'm trying {{ app.request.get('filter[_per_page]') }} but it's always empty...

Thanks !

Edit : I'm in javascript an i want to assign this result to a javascript variable like : var param = "{{ app.request.get('filter[_per_page]') }}";

4
Are you trying to access the query parameters from a subrequest (i.e. embedding controller)? try to call {{app.request.query.all}} and see what print. Hope this help.Matteo

4 Answers

29
votes

You must manage as an array accessing to the filter element as:

{{ app.request.get('filter')['_per_page'] }}

(This time I try before posting...)

8
votes

You've almost got it.

app object is GlobalVariables instance. When you say app.request, getRequest() is being invoked and returns an instance of standard Request object.

Now if you look at Request::get() (link) there is:

get(string $key, mixed $default = null, bool $deep = false)

I think what you need to do is this:

{{ app.request.get('filter[_per_page]', NULL, true) }}

Where NULL is default value and true means deep traversal of Request object.

0
votes

I found a solution like this :

app.request.attributes.get('request').query.get('param_name')
0
votes

To access to array of query params you can use:

app.request.attributes.get('_route_params');

You can see different solutions on the documentation.