JMSSerializerBundle 复杂生成的值

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返回的是完整大小的图片url。我想在向API客户端发送响应之前对此URL进行预处理,以便我可以生成所需图像的调整大小版本的新URL。我已经在SF2中有一个可以完成此任务的服务:
$resized_url = $someService->generateResizedUrl($item->getMainPhoto(), 640, 480);

但我不知道如何在JMSSerializer中使用此服务。也许有一些回调函数可以用于FOSRestBundle\JMSSerializerBundle,在它发送响应之前运行?

2个回答

1

1
请查看文档。有许多事件和/或注释可用于连接到序列化过程中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接