Complex generated value JMSSerializerBundle

I need to implement a RESTful API for my symfony 2 site, so I use FOSRestBundle + JMSSerializerBundle

I have a serializer yml like this for my object:

Acme\DemoBundle\Entity\Product: exclusion_policy: ALL accessor_order: custom custom_accessor_order: [id, title] properties: id: expose: true title: expose: true virtual_properties: getMainPhoto: serialized_name: photo 

The problem is that getMainPhoto returns me the url to the full size image. I want to preprocess this url before sending a response to the api client, where I can generate a new url to resize such an image. I already have a service in sf2 that can do this work:

 $resized_url = $someService->generateResizedUrl($item->getMainPhoto(), 640, 480); 

But I do not know how to use this service with JMSSerializer. Maybe there are some callbacks for FOSRestBundle \ JMSSerializerBundle before sending a response?

+6
symfony jmsserializerbundle fosrestbundle
Jan 23 '13 at 8:20
source share
2 answers

See the documentation . There are a number of events and / or annotations that you can use to connect to the serialization process.

+1
Jan 23 '13 at 10:21
source share

You can exclude the original URL and then add the changed URL using the http://jmsyst.com/libs/serializer/master/event_system#serializer-post-serialize event.

You need to write a listener that listens when Product instances are serialized.

+1
Apr 29 '13 at 21:02
source share



All Articles