Symfony2: Echoing JSON From a Controller for Use in an ExtJS 4 Grid
Asked Answered
B

6

10

I'm just getting started with Symfony2 and I'm trying to figure out what the correct approach is for echoing out JSON from a controller (e.g., People) for use in an ExtJS 4 grid.

When I was doing everything using a vanilla MVC approach, my controller would have method called something like getList that would call the People model's getList method, take those results and do something like this:

<?php
class PeopleController extends controller {
    public function getList() {
        $model = new People();
        $data = $model->getList();
        echo json_encode(array(
            'success' => true,
            'root' => 'people',
            'rows' => $data['rows'],
            'count' => $data['count']
        ));
    }
}
?>
  • What does this kind of behavior look like in Symfony2?
  • Is the controller the right place for this kind of behavior?
  • What are the best practices (within Symfony) for solving this kind of problem?
Beelzebub answered 5/2, 2012 at 2:35 Comment(0)
M
12

Is the controller the right place for this kind of behavior?

Yes.

What does this kind of behavior look like in Symfony2?

What are the best practices (within Symfony) for solving this kind of problem?

In symfony it looks pretty much alike, but there are couple of nuances.

I want to suggest my approach for this stuff. Let's start from routing:

# src/Scope/YourBundle/Resources/config/routing.yml

ScopeYourBundle_people_list:
    pattern:  /people
    defaults: { _controller: ScopeYourBundle:People:list, _format: json }

The _format parameter is not required but you will see later why it's important.

Now let's take a look at controller

<?php
// src/Scope/YourBundle/Controller/PeopleController.php
namespace Overseer\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class PeopleController extends Controller
{   
  public function listAction()
  {
    $request = $this->getRequest();

    // if ajax only is going to be used uncomment next lines
    //if (!$request->isXmlHttpRequest())
      //throw $this->createNotFoundException('The page is not found');

    $repository = $this->getDoctrine()
          ->getRepository('ScopeYourBundle:People');

    // now you have to retrieve data from people repository.
    // If the following code looks unfamiliar read http://symfony.com/doc/current/book/doctrine.html
    $items = $repository->findAll();
    // or you can use something more sophisticated:
    $items = $repository->findPage($request->query->get('page'), $request->query->get('limit'));
    // the line above would work provided you have created "findPage" function in your repository

    // yes, here we are retrieving "_format" from routing. In our case it's json
    $format = $request->getRequestFormat();

    return $this->render('::base.'.$format.'.twig', array('data' => array(
      'success' => true,
      'rows' => $items,
      // and so on
    )));
  }
  // ...
}    

Controller renders data in the format which is set in the routing config. In our case it's the json format.

Here is example of possible template:

{# app/Resourses/views/base.json.twig #}
{{ data | json_encode | raw }}

The advantage of this approach (I mean using _format) is that it if you decide to switch from json to, for example, xml than no problem - just replace _format in routing config and, of course, create corresponding template.

Methacrylate answered 5/2, 2012 at 8:31 Comment(1)
Actually I did. See the last code block {{ data | json_encode | raw }}Methacrylate
M
8

I would avoid using a template to render the data as the responsibility for escaping data etc is then in the template. Instead I use the inbuilt json_encode function in PHP much as you have suggested.

Set the route to the controller in the routing.yml as suggested in the previous answer:

ScopeYourBundle_people_list:
pattern:  /people
defaults: { _controller: ScopeYourBundle:People:list, _format: json }

The only additional step is to force the encoding in the response.

<?php
class PeopleController extends controller {
    public function listAction() {
        $model = new People();
        $data = $model->getList();
        $data = array(
            'success' => true,
            'root' => 'people',
            'rows' => $data['rows'],
            'count' => $data['count']
        );
        $response = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
    }
}
?>
Magistral answered 5/2, 2012 at 9:46 Comment(1)
Just a small side note, if the route specify the _format option correctly, the Content-Type header is set automatically by Symfony2 as it can be seen in the Response class here. Since, in this example, the format is json, the Content-Type header with value 'application/json' will be appended to the response.Dumortierite
K
1

To use return new JsonResponse(array('a' => 'value', 'b' => 'another-value'); you need to use the right namespace:

use Symfony\Component\HttpFoundation\JsonResponse;

As described here: http://symfony.com/doc/current/components/http_foundation/introduction.html#creating-a-json-response

Keister answered 4/12, 2013 at 22:41 Comment(0)
E
1

Instead of building your own response you can also use the built-in JsonResponse.

You define the route like in the other answers suggested:

ScopeYourBundle_people_list:
pattern:  /people
defaults: { _controller: ScopeYourBundle:People:list, _format: json }

And use the new response type:

<?php
class PeopleController extends controller {
    public function listAction() {
        $model = new People();
        $data = $model->getList();
        $data = array(
            'success' => true,
            'root' => 'people',
            'rows' => $data['rows'],
            'count' => $data['count']
        );
        return new \Symfony\Component\HttpFoundation\JsonResponse($data);
    }
}

For more information see the api or the doc (version 2.6).

Eileneeilis answered 20/2, 2015 at 10:22 Comment(0)
Y
0

Simple. Use FOSRestBundle and only return the People object from the controller.

Yoho answered 28/5, 2013 at 21:7 Comment(0)
I
-2

use

    return JsonResponse($data, StatusCode, Headers);
Infighting answered 14/8, 2013 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.