Twig get url parameter using []

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

How can I get these parameters with a twig?

I try {{ app.request.get('filter[_per_page]') }} but it is always empty ...

Thanks!

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

+8
php symfony twig
source share
2 answers

You should manage as an array accessing the filter element, as:

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

(This time I try before posting ...)

+15
source share

You almost got it.

An object

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

Now, if you look at Request::get() ( link ):

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

I think you need to do the following:

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

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

+4
source share

All Articles