Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Restful Post: JMSSerializerBundle vs Symfony Form Components

As far as i understand it right JMSSerializerBundle's deserialisation does the same same as the symfony form component when a controller gets an post/put/patch request?

So either i create a symfony custom formType for e.g. an UserType and when i get a request i do something like $form->handleRequest($request) or i use JMSSerializerBundle to unserialize the request to a document/entity which gets finally stored.

Does anyone have experience with both methods? Currently i'm only familiar with the form way... Which one should i choose?

The Application i'm talking about is purely Restful, there are no twig html templates and FOSRestbundle is doing all the RESTful routing.

like image 550
Thomas Spycher Avatar asked Jul 04 '14 10:07

Thomas Spycher


2 Answers

In our restfull API we usually use the Symfony Serializer component to handle the deserialization of entities, then the Symfony Validator component to ensure that the entities fulfill all the required conditions before pushing/updating them in database. Works pretty well, lighter than the form component.

Anyway The Form component would not be able to deserialize the json/xml so you'll have to use a serializer.

like image 157
rolebi Avatar answered Sep 30 '22 19:09

rolebi


The benefit of the Symfony\Form component over the JMS Serializer is that the validation is done before deserialization which fits into PHP 7 strict typing. Example case - you pass an array instead of a string, JMS creates and object and the getter raises a \TypeError instead of a validation error from the validator.

like image 31
Krzysztof Wesołowski Avatar answered Sep 30 '22 17:09

Krzysztof Wesołowski