FOSRestBundle setup for return JSON but still asking for Twig template FOSRestBundle setup for return JSON but still asking for Twig template symfony symfony

FOSRestBundle setup for return JSON but still asking for Twig template


As guys suggested: only Accept header or extension could give you a JSON. Seems like you've got this sorted with Accept header.

In order to use extension you must tell how do you want to set format things in Symfony.

This code should give you an output you want:

namespace RestTestBundle\Controller;use FOS\RestBundle\Controller\Annotations\View;use FOS\RestBundle\Controller\Annotations\Get;class YourController{    /**     * @Get("/api/v1/reps.{_format}", defaults={"_format"="json"})     * @View()     */    public function indexAction()    {        return array(            'status' => 'ok',            'companies' => array(                array('id' => 5),                array('id' => 7),            ),        );    }}

Edit1: if you do not want to use a View class, but pure arrays: do not forget to disallow View handling of SensioExtraBundle

sensio_framework_extra:    view:    { annotations: false }

Edit2: If you do not use HTML format and only want to have a json output you can use such fonfiguration:

fos_rest:    # ....    format_listener:        rules:            - { path: ^/, priorities: [ json ], fallback_format: json, prefer_extension: true }    # ....

Explanation why you do see an error "view not found":

TL;DR: your browser send an Accept header that tells FOSRestBundle to output a 'html' variant.

Background: This bundle works mostly with Accept headers, it's a good practice to have all possible output formats that are available: html (you can test your REST API with forms you provide, lists of objects, details of objects easily this way), json, xml. Sometimes even image mime types like image/jpeg, image/png as default or json/xml as a variant (you can use base64 image representation).

Explanation: If you open up a "network" tab of a browser and check out headers it sends you will notice something like: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 which means "use in such order":

  1. text/html
  2. application/xhtml+xml
  3. application/xml with priority of 0.9 which is forbidden according to your configuration
  4. */* with priority 0.8 which meand any format

If you look close to this is you will see that according to your configuration text/html is one of variants that your configuration has ('html') and */* is another one ('json'), but text/html has a priority of 1, while */* has a priority of 0.8, so text/html matches and FOSRestBundle tries to find a HTML representation and fails.

PS: If you post question multiple times - please make sure you watch for all responses in every thread.


You can give response in two ways

return View::create($entities, Codes::HTTP_OK);

or

$view = $this->view($entities, Codes::HTTP_OK);    return $this->handleView($view)


You can use simply

            $view = new View($form);            $view->setFormat('json');            return $this->handleView($view);