如何从Symfony2中的PUT请求获取参数?

bst*_*mpi 6 php symfony symfony-2.3

我正在使用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");
}
Run Code Online (Sandbox Code Playgroud)

这条路线:

some_bundle_adduserpage:
pattern: /user
defaults: { _controller: SomeBundle:User:add }
methods: [POST]
Run Code Online (Sandbox Code Playgroud)

它工作得很好.我也有这个方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

这条路线:

some_bundle_edituserpage:
pattern: /user/{id}
defaults: { _controller: SomeBundle:User:edit }
methods: [PUT]
requirements:
    id: \d+
Run Code Online (Sandbox Code Playgroud)

它不起作用.我可以提出一些要求而且POST很好,但是PUT没有用.具体来说,看起来我没有得到任何参数$this->getRequest().为什么$this->getRequest()似乎适用POST,但不是PUT?我究竟做错了什么?

bst*_*mpi 1

这做到了这一点:

$form = $this ->createForm(new UserType(), $user, array('method' => 'PUT'));

钻头method不见了。显然,Symfony 还不够酷,无法为您获取参数。创建表单时,您必须手动告诉它正在处理什么类型的请求。虽然这有效,但我不确定这是最好的答案。我非常愿意听听别人的意见。