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


i'm trying build simple restful-service symfony2 , fosrestbundle.

if send request get or post method response comes expected.

if use put or patch method result empty.

formtype

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 ''; } } 

my controller's putaction($id) , postaction($id) same.

controller/action

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(),     ); } 

result put request:

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

result post request:

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

the output of console route:debug

new_profiles                             /profiles/new.{_format}            get_profiles                             /profiles/{id}.{_format}           get_locations                            /locations.{_format}               get_location                             /locations/{id}.{_format}          post_locations                    post       /locations.{_format}               put_location                      put        /locations/{id}.{_format}          get_images                               /images.{_format}    

since know request going put request, seems silly use following :

  $method=$request->getmethod(); 

instead try use :

  $method= 'put'; 

moreover, $request object should passed parameter in action instead of retrieving request object, , instead of using :

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

you should use :

$form->handlerequest($request); 

to sum here code use :

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(),     ); } 

don't forget proper use statement request object.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -