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:

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

First question is :

  • does index method need to be an action method ? "indexAction" the method will output json.
  • how can I get the value of var1 and var2 etc ... ?
  • thanks !


    1)
    Yes it needs to be an action inside of a controller. If you return a JSON body you can use the JsonResponse.

    2)
    You just need to change the pattern of your action

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

    And in your MarkersController you add an action like this:

    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:

    public function indexAction(Request $request, $slug) {
        $var2 = $request->query->get('var2');
        // and so on...
    }
    
    链接地址: http://www.djcxy.com/p/58898.html

    上一篇: PHP执行函数分配给一个变量时

    下一篇: 在symfony2中获取许多变量