JMSSerializerBundle复杂生成的值

时间:2013-01-23 08:20:44

标签: symfony fosrestbundle jmsserializerbundle

我需要在symfony 2上为我的网站实现RESTful API,所以我使用FOSRestBundle + JMSSerializerBundle

我的实体有这样的序列化器yml:

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

问题是getMainPhoto将我的网址返回到全尺寸图片。我希望在向api客户端发送响应之前预处理此URL,我可以生成新的url来调整此类映像的大小。我已经在sf2中有服务可以完成这项工作:

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

但我不知道如何在JMSSerializer中使用此服务。也许在发送响应之前有一些FOSRestBundle \ JMSSerializerBundle的回调?

2 个答案:

答案 0 :(得分:1)

看看the documentation。您可以使用许多事件和/或注释来连接序列化过程。

答案 1 :(得分:1)

您可以排除原始网址,然后使用http://jmsyst.com/libs/serializer/master/event_system#serializer-post-serialize事件添加已调整大小的网址。

你必须编写一个侦听器,在“Product”实例被序列化时监听。