0

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Sinan Samet
  • 6,432
  • 12
  • 50
  • 93
  • What you are trying to do exactly? what is this variable messageModel? where is it defined? please be more clear – iiirxs Sep 19 '18 at 18:37
  • messageModel inserts the posted parameters into the database. What I'm trying to do is catch $messageId and $comment as post. But how do I define those parameters? – Sinan Samet Sep 19 '18 at 18:39

1 Answers1

2

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

iiirxs
  • 4,493
  • 2
  • 20
  • 35