Can't get POST parameters in controller

4.6k Views Asked by At

I'm experimenting with Symfony and I am trying to make a POST request. But I can't seem to get the request's POST parameters inside my controller.

/**
 * @Route("/messages/comment/post/", methods={"POST"}, name="postComment")
 */
public function postComment($messageId, $comment)
{
    $statuscode = 200;
    $response = null;
    try {
        $response = $this->messageModel->postComment($messageId, $comment);
    } catch (\PDOException $exception) {
        var_dump($exception);
        $statuscode = 500;
    }
    return new JsonResponse($response, $statuscode);
}

How do I define parameters for this?

1

There are 1 best solutions below

1
On BEST ANSWER

To get POST parameters inside a controller you should use:

use Symfony\Component\HttpFoundation\Request;
....
/**
 * @Route("/messages/comment/post/", methods={"POST"}, name="postComment")
 */
public function postComment(Request $request){

    $messageId = $request->request->get('messageId');
    $comment = $request->request->get('comment');
    ...
}

Documentation here