6

我需要在 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 在它发送响应之前有一些回调?

4

2 回答 2

1

看看文档。您可以使用许多事件和/或注释来挂钩序列化过程。

于 2013-01-23T10:21:47.623 回答
1

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

您必须编写一个侦听器,在“产品”实例被序列化时进行侦听。

于 2013-04-29T21:02:39.093 回答