Symfony2 bundle allows to use templates for json responses

Installs: 41

Dependents: 0

Stars: 1

Watchers: 1

Type: symfony-bundle

1.0.1 2014-10-17 18:36 UTC


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


Add bundle with composer:

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

Enable php templating engine in your symfony config, register bundle in AppKernel.php :

// app/AppKernel.php

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


Example controller:


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:

// 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(),


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)

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


This bundle is under the MIT license.