user6216601
user6216601

Reputation:

Write GET route in Symfony with FOSRestBundle

I have function

/**
 * My function
 *
 * @FOS\View()
 * @FOS\Get("/myfunction/?param1={p1}&param2={p2}&paramn={pn}")
 *
 * @param integer $param1
 * @param integer $param2
 * @param integer $paramn
 * @return mixed
 */
public function getMyFunction($param1, $param2, $paramn)
{
    return new Response($param1. ' ' . $param1. ' ' . $paramn);
}

But when I call http://host/myfunction/?param1=1&param1=2&paramn=3 dosen't work.

What is wrong in definition of function?


UPDATE: New function

 /**
 * My function
 *
 * @FOS\View()
 * @FOS\Get("/myfunction/")
 *
 * Request $request
 * @return mixed
 */
public function getMyFunction(Request $request)
{
    $requestParams = $request->request->all();

    return new Response($requestParams['param1']);
}

And, now I call http://host/myfunctin/?param1=1, but still, dosen't work.

Error: "Notice: Undefined index: param1"

Request for get parameters isn't good?

Thanks!

Upvotes: 0

Views: 42

Answers (1)

Carlos
Carlos

Reputation: 1431

You have to remove the query string parameters from the route.

To get them you have to inject a Request object in the function signature and the use $request->get('parametername') to retrieve then.

Upvotes: 1

Related Questions