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
Twig get url parameter with []
提问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 filter
element as:
您必须作为访问filter
元素的数组进行管理:
{{ app.request.get('filter')['_per_page'] }}
(This time I try before posting...)
(这次我在发帖之前尝试...)
回答by Jovan Perovic
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.
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 NULL
is default value and true
means deep
traversal of Request
object.
哪里NULL
是默认值,true
表示deep
遍历Request
对象。