代码之家  ›  专栏  ›  技术社区  ›  Sinan Samet

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

  •  -1
  • Sinan Samet  · 技术社区  · 7 年前

    我正在试验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 回复  |  直到 7 年前
        1
  •  2
  •   iiirxs    7 年前

    要在控制器内获取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

    推荐文章