Symfony JMSSerializerBundle复杂生成值

Symfony JMSSerializerBundle复杂生成值,symfony,fosrestbundle,jmsserializerbundle,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:

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

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

您可以排除原始url,然后使用事件添加调整大小的url

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