无法在控制器中获取 POST 参数
Can't get POST parameters in controller
我正在尝试使用 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);
}
如何为此定义参数?
要在控制器中获取 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
我正在尝试使用 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);
}
如何为此定义参数?
要在控制器中获取 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