本文介绍了JMSSerializerBundle 复杂生成值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need implement RESTful API for my site on symfony 2, so i use FOSRestBundle + JMSSerializerBundle

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

I have such serializer yml for my entity:

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 中提供了可以完成这项工作的服务:

The problem is that getMainPhoto return me url to full sized image. I want preprocess this url before sending response to api client where i can generate new url to resized version of such image. I already have service in sf2 which can do this job:

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

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

But i don't know how can i use this service with JMSSerializer. Maybe there is some callbacks for FOSRestBundle\JMSSerializerBundle just before it send response?

推荐答案

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

Have a look at the documentation. There are is a number of events and/or annotations you can use to hook into the serialization process.

这篇关于JMSSerializerBundle 复杂生成值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!