FosRestBundele PUT方法没有';不能使用Forms,但POST或GET可以完美工作


FosRestBundele PUT Method doesn't work with Forms but POST or GET works perfectly

我正试图用Symfony2和FosRestBundle构建一个简单的Restful服务。

如果我使用GETPOST方法发送请求,则响应将按预期进行。

如果我使用PUTPATCH方法,则结果为空。

表单类型

namespace MobileService'UserBundle'Form;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
use Symfony'Component'OptionsResolver'OptionsResolverInterface;
class CurrentLocationType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('latitude')
        ->add('longitude')
        ->add('city')
        ->add('zip')
    ;
}
/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MobileService'UserBundle'Entity'CurrentLocation',
        'csrf_protection' => false
    ));
}
/**
 * @return string
 */
public function getName()
{
    return '';
}
}

我的控制器的putAction($id)postAction($id)完全相同。

控制器/操作

public function putAction($id)
{
    $request = $this->getRequest();
    $method=$request->getMethod();
    $em = $this->getDoctrine()->getManager();
    $location = $em->getRepository('MobileServiceUserBundle:CurrentLocation')->find($id);
    $form = $this->createForm(new CurrentLocationType(), $location, array('method' => $method));
    $form->submit($request, false);
    if ($form->isValid()) {
        $em->persist($location);
        $em->flush();
    }
    else die('Invalid form.');
    return array(
        'location' => $form->getData(),
    );
}

PUT请求的结果:

{"location":{"id":1,"latitude":0,"longitude":0,"city":"","zip":0}}

POST请求的结果:

{"location":{"id":1,"latitude":51.4681,"longitude":6.9174,"city":"Essen","zip":451361}}

控制台路由的输出:debug

new_profiles                      GET    ANY    ANY  /profiles/new.{_format}           
get_profiles                      GET    ANY    ANY  /profiles/{id}.{_format}          
get_locations                     GET    ANY    ANY  /locations.{_format}              
get_location                      GET    ANY    ANY  /locations/{id}.{_format}         
post_locations                    POST   ANY    ANY  /locations.{_format}              
put_location                      PUT    ANY    ANY  /locations/{id}.{_format}         
get_images                        GET    ANY    ANY  /images.{_format}   

既然您知道您的请求将是PUT请求,那么使用以下内容似乎很愚蠢:

  $method=$request->getMethod();

相反,尝试使用这个:

  $method= 'PUT';

此外,$request对象应该在您的操作中作为参数传递,而不是从请求对象中检索它,也不是使用以下内容:

$form->submit($request, false);

你应该使用这个:

$form->handleRequest($request);

总之,这里是我要使用的代码:

public function putAction($id, Request $request)
{
    $method='PUT';
    $em = $this->getDoctrine()->getManager();
    $location = $em->getRepository('MobileServiceUserBundle:CurrentLocation')->find($id);
    $form = $this->createForm(new CurrentLocationType(), $location, array('method' => $method));
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em->persist($location);
        $em->flush();
    }
    else die('Invalid form.');
    return array(
        'location' => $form->getData(),
    );
}

不要忘记Request对象的正确使用语句。

并非所有浏览器都支持PUT|PATCH|DELETE请求。

例如,即使表单具有method="PUT",他们通常也会发送POST请求。

解决方案:

Symfony对此有一个简单的解决方法。

在表单中添加一个名为_method、值为PUTPATCH的隐藏输入字段。

您可以在以下两篇食谱文章中阅读更多关于更改请求方法的信息:

  • 更改窗体的动作和方法
  • 用_Method伪造方法