php Twig 使用 [] 获取 url 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28456853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:52:14  来源:igfitidea点击:

Twig get url parameter with []

phpsymfonytwig

提问by Clément Andraud

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

我有一个像这样的网址: 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...

我正在尝试,{{ app.request.get('filter[_per_page]') }}但它总是空的...

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]') }}";

编辑:我在 javascript 中,我想将此结果分配给一个 javascript 变量,例如: var param = "{{ app.request.get('filter[_per_page]') }}";

回答by Matteo

You must manage as an array accessing to the filterelement as:

您必须作为访问filter元素的数组进行管理:

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

(This time I try before posting...)

(这次我在发帖之前尝试...)

回答by Jovan Perovic

You've almost got it.

你已经差不多了。

appobject is GlobalVariablesinstance. When you say app.request, getRequest()is being invoked and returns an instance of standard Requestobject.

app对象是GlobalVariables实例。当您说app.request,getRequest()被调用并返回标准Request对象的实例时。

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

现在如果你看Request::get()链接)有:

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 NULLis default value and truemeans deeptraversal of Requestobject.

哪里NULL是默认值,true表示deep遍历Request对象。