How to make JMS Serializer and LiipImagine works together

API json

I’m working on writing the API for the front-end on AngularJS. To do this, I use the standard bundles set for Symfony:

FOSRestBundle
JMSSerializerBundle
NelmioApiDocBundle

I had a task to give a reference to thumb and preview in objects that contain pictures. I have already used in the code LiipImagineBundle and for generating links in api also wanted to use this bundle. After a search for the right solution to me, I was surprised that there are no ready-made bundle for this.

Just want to say that I am obviously not a fan of JMSSerializerBundle, as the project is not actively supported giving some limitations when working with it. Symfony Serializer will be better, but life is not so simple) Also, please note that the API, what I write, is scheduled to be open, so it have to be simple for understanding.

Let us return to the subject. Realizing that I need to write my bike I started writing it) As the project for which it was needed not a little, I had to support different configurations. For example the picture may have several filters or one. Since I use to upload data on server VichUploaderBundle, I need make the support of generation links given by this bundle.

In JMSSerializerBundle there are two events that are invoked when the object serializing:

  • serializer.pre_serialize used if we need to change some information in the object. For example in object are picture field with reference to the original picture, and you need to change field data to thumb url.
  • serializer.post_serialize used if you want to add additional information to the  serialized object. For example there are a field image with a link to the original image and it can not be overwritten because it is already used somewhere then we can add a new field image_thumb for example, and put in it a reference to the thumb.

Documentation and bundle are on GitHub LiipImagineSerializationBundle.