Symfony Restful Post: JMSSerializerBundle vs Symfo

2019-05-02 11:45发布

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.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-02 12:24

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.

查看更多
【Aperson】
3楼-- · 2019-05-02 12:31

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.

查看更多
登录 后发表回答