Не удается получить параметры POST в контроллере - PullRequest
0 голосов
/ 19 сентября 2018

Я экспериментирую с Symfony и пытаюсь сделать POST-запрос.Но я не могу получить параметры запроса POST внутри моего контроллера.

/**
 * @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);
}

Как мне определить параметры для этого?

1 Ответ

0 голосов
/ 19 сентября 2018

Чтобы получить параметры POST внутри контроллера, вы должны использовать:

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');
    ...
}

Документация здесь

...