无法在控制器中获取POST参数

时间:2018-09-19 17:55:14

标签: php symfony symfony-routing

我正在尝试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 :(得分:2)

要在控制器内获取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');
    ...
}

文档here