简体   繁体   English

PUT请求不使用Symfony表单和FosRest

[英]PUT request not working with Symfony forms and FosRest

I'm trying to write a simple restful controller for user management in Symfony using FosRest and Symfony forms. 我正在尝试使用FosRest和Symfony表单在Symfony中为用户管理编写一个简单的restful控制器。 My application is backed by Amazon DynamoDB, although I don't think it matters. 我的应用程序由Amazon DynamoDB支持,尽管我认为不重要。

I have DELETE, GET and POST (new user) all working perfectly. 我有DELETE,GET和POST(新用户)都完美无缺。

I've now come to writing the PUT action (edit user) which doesn't seem to work. 我现在来写PUT动作(编辑用户)似乎不起作用。 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. 为了创建PUT,我基本上复制了POST操作,但是修改它以首先加载旧对象。

In the POST, the User object automatically gets populated by the line $form->handleRequest($request); 在POST中,User对象自动由$ form-> handleRequest($ request)行填充;

This doesn't seem to be working in the PUT action, the user object doesn't get populated/modified. 这似乎不适用于PUT操作,用户对象不会被填充/修改。 I've checked the $_REQUEST array and the data is being submitted. 我检查了$ _REQUEST数组,并且正在提交数据。 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). 由于缺少对PUT的浏览器支持,我通过使用查询参数_method = PUT对数据执行POST来调用操作(对DELETE工作正常,并且它将路由到正确的位置)。

Here is my POST action that works: 这是我的POST动作有效:

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: 这是我的PUT控制器没有:

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. 任何帮助将不胜感激。

Okay, after some help in the Symfony IRC channel, it turns out the reason this isn't working is this part of the code: 好的,在Symfony IRC频道的一些帮助之后,事实证明这不起作用的原因是这部分代码:

$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. 由于PUT在许多浏览器中不起作用,我试图将方法设置为POST并将_method = PUT作为查询参数传递以覆盖真正的HTTP方法。 It turns out there is no need to do this, and Symfony will handle it all for you. 事实证明没有必要这样做,Symfony将为您处理这一切。 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. 通过这样做,Symfony实际上呈现了一个带有隐藏_method字段的POST表单 - 无需手动执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM