2017 © Pedro Peláez
 

symfony-bundle json-response-bundle

Symfony2 bundle allows to use templates for json responses

image

bu/json-response-bundle

Symfony2 bundle allows to use templates for json responses

  • Monday, October 2, 2017
  • by Fludimir
  • Repository
  • 1 Watchers
  • 0 Stars
  • 73 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

BuJsonResponseBundle

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., (*1)

Based on @Template annotation from SensioFrameworkExtraBundle and requires it., (*2)

Build Status, (*3)

Installation

Add bundle with composer:, (*4)

composer require bu/json-response-bundle dev-master, (*5)

Enable php templating engine in your symfony config: ``` yaml, (*6)

app/config/config.yml

framework: # ... templating: engines: ['twig', 'php'], (*7)


register bundle in AppKernel.php : ``` php <?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Bu\JsonResponseBundle\BuJsonResponseBundle(), ); }

Usage

Example controller: ``` php <?php, (*8)

namespace Application\MyBundle\Controller;, (*9)

use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Bu\JsonResponseBundle\Configuration\JsonResponseTemplate;, (*10)

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

Json template for listAction:
``` php
<?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: ``` php use Bu\JsonResponseBundle\HttpFoundation\JsonResponse;, (*12)

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, (*13)

License

This bundle is under the MIT license., (*14)

The Versions

02/10 2017

dev-master

9999999-dev http://github.com/Fludimir/JsonResponseBundle

Symfony2 bundle allows to use templates for json responses

  Sources   Download

MIT

The Requires

 

by Avatar Fludimir

template json bundle

17/10 2014

1.0.1

1.0.1.0 http://github.com/Fludimir/JsonResponseBundle

Symfony2 bundle allows to use templates for json responses

  Sources   Download

MIT

The Requires

 

by Avatar Fludimir

template json bundle

24/06 2013

1.0.0

1.0.0.0 http://github.com/Fludimir/JsonResponseBundle

Symfony2 bundle allows to use templates for json responses

  Sources   Download

MIT

The Requires

 

by Avatar Fludimir

template json bundle