dsfphczao23473056 2013-12-17 11:39 采纳率: 100%
浏览 61
已采纳

PUT请求不使用Symfony表单和FosRest

I'm trying to write a simple restful controller for user management in Symfony using FosRest and Symfony forms. My application is backed by Amazon DynamoDB, although I don't think it matters.

I have DELETE, GET and POST (new user) all working perfectly.

I've now come to writing the PUT action (edit user) which doesn't seem to work. I've spent ages banging my head against a brick wall and I just can't work it out.

In order to create the PUT, I essentially copied the POST action but modified it to load the old object first.

In the POST, the User object automatically gets populated by the line $form->handleRequest($request);

This doesn't seem to be working in the PUT action, the user object doesn't get populated/modified. I've checked the $_REQUEST array and the data is being submitted. Because of the lack of browser support for PUT, I'm calling the action by doing a POST of the data with the query parameter _method=PUT (which works fine for DELETE, and it is routing to the correct place).

Here is my POST action that works:

public function postUsersAction(Request $request)
{

    $user = new User();
    $user->setTable($this->getTable());

    $formBuilder = $this->createFormBuilder($user, array(
        'validation_groups' => array('registration', '')))
        ->add('username', 'text')
        ->add('password', 'password')
        ->setAction($this->generateUrl('post_users'))
        ->setMethod('POST')
        ->setAttribute('validation_groups', array('registration'));
    $roles = $this->getFlattenedRoles($this->getRoles());

    $formBuilder->add('roles', 'choice', array(
        'choices'   => $roles,
        'multiple'  => true,
        'expanded'  => true
    ));

    $form = $formBuilder->add('save', 'submit')->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {
        $user->save();
        $params = array('user' => $user);
        $view = $this->view($params, 200)
            ->setTemplate("MyRestBundle:User:newconfirm.html.twig");
        return $this->handleView($view);
    }

    $params = array('form' => $form, 'user' => $user);
    $view = $this->view($params, 400)
        ->setTemplate("MyRestBundle:User:new.html.twig");
    return $this->handleView($view);
}

Here is my PUT controller that doesn't:

public function putUserAction($slug, Request $request)
{
    $table = $this->getTable();
    $user = $table->load($slug);

    $formBuilder = $this->createFormBuilder($user)
        ->add('password', 'password')
        ->setAction($this->generateUrl('put_user', array('slug' => $slug, '_method' => 'PUT')))
        ->setMethod('POST');
    $roles = $this->getFlattenedRoles($this->getRoles());

    $formBuilder->add('roles', 'choice', array(
        'choices'   => $roles,
        'multiple'  => true,
        'expanded'  => true
    ));

    $form = $formBuilder->add('save', 'submit')->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {
        $user->save();
        $params = array('user' => $user);
        $view = $this->view($params, 200)
            ->setTemplate("MyRestBundle:User:newconfirm.html.twig");
        return $this->handleView($view);
    }

    $params = array('form' => $form, 'user' => $user);
    $view = $this->view($params, 400)
        ->setTemplate("MyRestBundle:User:new.html.twig");
    return $this->handleView($view);
}

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanjuda5789 2013-12-17 14:01
    关注

    Okay, after some help in the Symfony IRC channel, it turns out the reason this isn't working is this part of the code:

    $formBuilder = $this->createFormBuilder($user)
        ->add('password', 'password')
        ->setAction($this->generateUrl('put_user', array('slug' => $slug, '_method' => 'PUT')))
        ->setMethod('POST');
    

    Since PUT doesn't work in many browsers, I was trying to set the method as POST and pass _method=PUT as a query parameter to override the real HTTP method. It turns out there is no need to do this, and Symfony will handle it all for you. The above code is now just:

    $formBuilder = $this->createFormBuilder($user)
        ->add('password', 'password')
        ->setAction($this->generateUrl('put_user', array('slug' => $slug))
        ->setMethod('PUT');
    

    By doing this, Symfony actually renders a POST form with a hidden _method field - there's no need to do anything manually.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记