我需要在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,在api客户端上我可以生成新的url来调整此类图像的大小。我已经在sf2中提供了服务,可以完成此工作:
$resized_url = $someService->generateResizedUrl($item->getMainPhoto(), 640, 480);

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

最佳答案

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

关于symfony - JMSSerializerBundle复杂生成的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14475171/

10-09 08:28