pulpmedia/serialized-response-bundle

一个简单的包,提供了一个简单的方式来发送序列化对象的json/xml/yaml响应。

安装: 8

依赖: 0

建议者: 0

安全: 0

星星: 5

关注者: 5

分支: 1

开放问题: 1

类型:symfony-bundle

dev-master / 1.1.x-dev 2014-08-03 15:33 UTC

This package is auto-updated.

Last update: 2024-09-21 00:28:59 UTC


README

一个简单的包,提供了一个简单的方式来发送序列化对象的json/xml/yaml响应。

##简介 这个包允许您直接从控制器返回可序列化的对象,以接收json/xml/yaml格式的序列化响应。它需要 jms/serializer-bundle

##安装 ###步骤 1 此库可以通过composer轻松安装

composer pulpmedia/serialized-response-bundle

或者直接将其添加到您的composer.json文件中。这将安装 pulpmedia/serialized-response-bundlejms/serializer,如果尚未安装的话。

###步骤 2 在kernel中启用该包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        new JMS\SerializerBundle\JMSSerializerBundle(), //If you have not already done this already
        // ...
        new Pulpmedia\SerializedResponseBundle\PulpmediaSerializedResponseBundle(),
    );
}

##使用 使用这个包非常简单。只需返回应该被序列化的对象,并使用相应的注释;

<?php
namespace My\Bundle\Controller

use Pulpmedia\SerializedResponseBundle\Annotation\SerializedResponse;

// ...

class MyController extents Controller{

  /**
   * @Route("/get/{id}")
   * @SerializedResponse(format="json/xml/yaml")
   */
  public function getAction($id){
    $em = $this->getDoctrine()->getManager();

    if($object = $em->getRepository('MyBundle:Entity')->find($id)){
        return $object;
    }
    return null;
  }
}