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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端