简体   繁体   English

$ _GET在symfony2中可以获得很多变量

[英]$_GET in symfony2 to get many variables

I'm trying to get the variables from a $_GET request the request is like /markers/var1/var2/var3/var4 the route file is as follows: 我正在尝试从$ _GET请求中获取变量,该请求就像/ markers / var1 / var2 / var3 / var4一样,路由文件如下:

Markers:
pattern:  /markers/{slug}
defaults: { _controller: ngNearBundle:Markers:index }

First question is : 第一个问题是:

  1. does index method need to be an action method ? 索引方法需要成为一种动作方法吗? "indexAction" the method will output json. “ indexAction”方法将输出json。
  2. how can I get the value of var1 and var2 etc ... ? 如何获得var1和var2等的值...?

thanks ! 谢谢 !

1) 1)
Yes it needs to be an action inside of a controller. 是的,它必须是控制器内部的动作。 If you return a JSON body you can use the JsonResponse . 如果返回JSON正文,则可以使用JsonResponse

2) 2)
You just need to change the pattern of your action 您只需要更改action

Markers:
    pattern: /markers/{slug}/{var2}/{var3}/{var4}
    defaults: { _controller: ngNearBundle:Markers:index }

And in your MarkersController you add an action like this: 然后在您的MarkersController添加如下操作:

public function indexAction($slug, $var2, $var3, $var4) {
    //...
}

Alternatively you can leave your route like this: /markers/{slug} , add the other variables as plain GET variables ( /markers/test?var2=a&var3=b&var4=c ) and access them in your action like this: 或者,您可以这样离开路线: /markers/{slug} ,将其他变量添加为纯GET变量( /markers/test?var2=a&var3=b&var4=c ),然后按以下action访问:

public function indexAction(Request $request, $slug) {
    $var2 = $request->query->get('var2');
    // and so on...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM