bu/json-response-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bu/json-response-bundle

Composer 安装命令:

composer require bu/json-response-bundle

包简介

Symfony2 bundle allows to use templates for json responses

README 文档

README

BuJsonResponseBundle helps you to use templates for json responses in just same way as templates are used for html responses, allowing you to separate business logic and data presentation when you need to return data in json format.

Based on @Template annotation from SensioFrameworkExtraBundle and requires it.

Build Status

Installation

Add bundle with composer:

composer require bu/json-response-bundle dev-master

Enable php templating engine in your symfony config:

# app/config/config.yml
framework:
    # ...
    templating:
        engines: ['twig', 'php']

register bundle in AppKernel.php :

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Bu\JsonResponseBundle\BuJsonResponseBundle(),
    );
}

Usage

Example controller:

<?php

namespace Application\MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Bu\JsonResponseBundle\Configuration\JsonResponseTemplate;


class ProductController extends Controller
{
    /**
     * @JsonResponseTemplate
     */
    public function listAction()
    {
        return array('products' => $this->get('my.product.service')->getAllProducts());
    }
}

Json template for listAction:

<?php
// Resources/view/Product/list.json.php

$data = array();
foreach ($products as $product) {
    $data[$product->getStatus()][] = array(
        'name'              => $product->getName(),
        'description'       => $product->getDescription(),
        'relationsCount'    => count($product->getRelations()),
        'isRequiresCheck'   => $product->isRequiresCheck(),
    );
}

$view['jsonResponse']->output($data);

Also there is simple JsonResponse class that can be used if you already have data prepared:

use Bu\JsonResponseBundle\HttpFoundation\JsonResponse;

    public function deleteAction(Product $product)
    {
        $this->get('my.product.service')->delete($product);

        return new JsonResponse(array('success' => true));
    }

As of Symfony 2.7 there is similar internal class Symfony\Component\HttpFoundation\JsonResponse https://symfony.com/doc/2.7/components/http_foundation.html#creating-a-json-response

License

This bundle is under the MIT license.

统计信息

  • 总下载量: 88
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-04-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固