I am currently writing a front-end API for AngularJS. For this, I use the standard symfony bundle:
I had a task to give links to thumb and preview in objects that contain pictures. I already used
LiipImagineBundle in the code and also wanted to use this bundle to generate links in api. After some search for the solution I needed, I was surprised that there are no ready-made bundles for this.
Just want to say that I am clearly not a fan of JMSSerializerBundle, since the project is not actively supported, which gives some limitations when working with it + it is not written entirely with regard to performance. Symfony Serializer will be better, but life is not so simple)
')
Also please note that the API is scheduled to be open and links to the thumb should be sent in the answer. Just write in the dock, use the url
http://example.com/resolver/filter/image/name.jpg for getting the thumb is not suitable, and not beautiful as that)
Let's return to the topic. Realizing that you need to write your bike, I started writing it. Since the project for which he needed was not a small one had to be done so that a different configuration would be supported. For example, the picture may have several filters and may not be. Since I use to load everything onto the
VichUploaderBundle server, I had to generate links, considering that the field being serialized could be the field of this bundle.
In JMSSerializerBundle there are two events that are raised when serializing the object serializer.pre_serialize and serializer.post_serialize that I used.
- serializer.pre_serialize Used if we need to change some information in the object. For example, there was a picture field with a link to the original picture, but you need to insert a link to the thumb in the place of it.
- serializer.post_serialize Used if you want to add additional information to the serialization result. For example, there is an image field with a link to the original image and it cannot be overwritten, since it is already used somewhere, then we can add a new image_thumb field for example and put a link to the thumb in it.
The documentation on using bundla and the bundle itself are on the GitHub
LiipImagineSerializationBundle .