I'm using Symfony 2.3, and I have a method that looks like this:
我用的是Symfony 2。3,我有一个方法是这样的:
public function addAction() {
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
try {
return $this
->render(
'SomeBundle:Default:adduser.html.twig',
array('id' => $user->getId()));
} catch (Exception $e) {
throw new HttpException(500, "Error persisting");
}
}
throw new HttpException(400, "Invalid request");
}
with this route:
这条路线:
some_bundle_adduserpage:
pattern: /user
defaults: { _controller: SomeBundle:User:add }
methods: [POST]
and it works just fine. I also have this method:
它运行得很好。我也有这个方法:
public function editAction($id) {
$response = new Response();
if (!$this->doesUserExist($id)) {
$response->setStatusCode(400);
return $response;
}
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($this->getRequest());
if (!$form->isValid()) {
$response->setStatusCode(400);
return $response;
}
$user->setId($id);
$em = $this->getDoctrine()->getManager();
try {
$em->persist($user);
$em->flush();
$response->setStatusCode(200);
} catch (Exception $e) {
$response->setStatusCode(500);
}
return $response;
}
with this route:
这条路线:
some_bundle_edituserpage:
pattern: /user/{id}
defaults: { _controller: SomeBundle:User:edit }
methods: [PUT]
requirements:
id: \d+
and it doesn't work. I can craft some request and POST
it just fine, but the PUT
doesn't work. Specifically, it looks like I'm not getting any parameters inside of $this->getRequest()
. Why does $this->getRequest()
seem to work for POST
, but not PUT
? What am I doing wrong?
它不工作。我可以设计一些请求并发布它只是很好,但是PUT不起作用。具体地说,看起来我在$this->getRequest()中没有得到任何参数。为什么$this->getRequest()似乎是用于POST,而不是PUT?我做错了什么?
2 个解决方案
#1
1
This did the trick:
这个诀窍:
$form = $this ->createForm(new UserType(), $user, array('method' => 'PUT'));
$form = $this ->createForm(新UserType(), $user, array('method' => 'PUT'));
The method
bit was missing. Apparently, Symfony isn't cool enough to just get the parameters for you. When creating a form, you have to manually tell it what type of request it's dealing with. While this worked, I'm not sure that this is the best answer. I'm more than willing to hear others.
方法位丢失了。显然,Symfony并没有酷到只给你参数。在创建表单时,您必须手动告诉它处理的是哪种类型的请求。虽然这个方法有效,但我不确定这是否是最好的答案。我非常愿意听别人说话。
#2
0
The PUT
method is streamed directly into the stdin
therefore to access it you should use php://input
and fopen
with read
.
PUT方法直接流到stdin中,因此要访问它,应该使用php://input和fopen。
$put = fopen("php://input", "r");
disclaimer there may be a built in method for this in Symfony, but I've forgotten.
在Symfony中,可能会有这样的方法,但我忘记了。
#1
1
This did the trick:
这个诀窍:
$form = $this ->createForm(new UserType(), $user, array('method' => 'PUT'));
$form = $this ->createForm(新UserType(), $user, array('method' => 'PUT'));
The method
bit was missing. Apparently, Symfony isn't cool enough to just get the parameters for you. When creating a form, you have to manually tell it what type of request it's dealing with. While this worked, I'm not sure that this is the best answer. I'm more than willing to hear others.
方法位丢失了。显然,Symfony并没有酷到只给你参数。在创建表单时,您必须手动告诉它处理的是哪种类型的请求。虽然这个方法有效,但我不确定这是否是最好的答案。我非常愿意听别人说话。
#2
0
The PUT
method is streamed directly into the stdin
therefore to access it you should use php://input
and fopen
with read
.
PUT方法直接流到stdin中,因此要访问它,应该使用php://input和fopen。
$put = fopen("php://input", "r");
disclaimer there may be a built in method for this in Symfony, but I've forgotten.
在Symfony中,可能会有这样的方法,但我忘记了。