Aysennoussi
Aysennoussi

Reputation: 3860

$_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 :

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

thanks !

Upvotes: 1

Views: 335

Answers (1)

ferdynator
ferdynator

Reputation: 6410

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...
}

Upvotes: 2

Related Questions