Handling POST requests using Symofny forms in FOSRestBundle

It is not clear to me how I should use the Symfony form component with FOSRestBundle for the POST endpoints that I use to create resources.

Here is what I got in my POST controller action:

//GuestController.php public function cpostAction(Request $request) { $data = json_decode($request->getContent(), true); $entity = new Guest(); $form = $this->createForm(GuestType::class, $entity); $form->submit($data); if ($form->isValid()) { $dm = $this->getDoctrine()->getManager(); $dm->persist($entity); $dm->flush(); return new Response('', Response::HTTP_CREATED); } return $form; } 

What am I doing:

  • Send the application/json POST request to the endpoint ( /guests );
  • Create an instance of the form that is associated with the entity ( Guest );
  • Due to the fact that I'm sending JSON, I need the json_decode body of the request before sending it to the form ( $form->submit($data) ).

Questions I have:

  • Should I always have json_decode() contents of the Request manually before submitting it to the form? Can this process be automated in some way using the FosRestBundle?
  • Is it possible to send application/x-www-form-urlencoded data to a controller action and process it with:

-

 $form->handleRequest($request) if ($form->isValid()) { ... } ... 

I could not get the above to work, an instance of the form was never submitted.

  1. Is there any advantage to using the form component using ParamConverter together with the validator directly - here is the idea:

-

 /** * @ParamConverter("guest", converter="fos_rest.request_body") */ public function cpostAction(Guest $guest) { $violations = $this->getValidator()->validate($guest); if ($violations->count()) { return $this->view($violations, Codes::HTTP_BAD_REQUEST); } $this->persistAndFlush($guest); return ....; } 

Thanks!

+5
source share
1 answer

I'm trying to do the same with you, and it's hard for me to find answers to this topic. I think that today it is an important issue for the development of an api-rest website for mobile applications. Anyway!

Please find my answer below:

Should I really always have json_decode () the contents of the request manually before submitting it to the form?

No, I get data like this $ params = $ request-> query-> all (); $ User-> setUsername ($ PARAMS ['fos_user_registration_form'] ['username']);

Is there any way to automate this process using the FosRestBundle?

I do not think so.

Is it possible to send application data / x -www-form-urlencoded to a controller action and process using

Yes, but now I'm trying to process my form, and I can’t do this.

Is there any advantage to using the form component using ParamConverter together with the validator directly - here is the idea:

No

I assume that I will not register the user only with: $ userManager = $ this-> get ('fos_user.user_manager');

and do my control manually.

I posted a problem here and I'm waiting: https://github.com/FriendsOfSymfony/FOSUserBundle/issues/2405

Did you manage to get additional information?

0
source

Source: https://habr.com/ru/post/1262033/


All Articles