Search code examples
jsonsymfonyfosrestbundle

FOSRestBundle Unable to find template, can't return json


I wan't to use FOSRestBundle to return json responses from api, but FOSRestBundle want's to return twig instead of json.

my controller method looks like this

/**
 * @Rest\Post("/producers-from-points",name="get-producers-from-points",options={"expose"=true})
 * @Rest\View(serializerGroups={"list"}, statusCode=200)
 *
 * @return \FOS\RestBundle\View\View
 * @throws \Exception
 */
public function getProducersFromPointsAction()
{
    $json = parent::getJsonFromRequest();
    if($json===false){
        throw new \Exception("");
    }

    /** @var EntityManager $em */
    $em = $this->getDoctrine()->getManager();

    $poss = $em->getRepository('AppBundle:PointOfSale')->getPointOfSalesByAroundPoints($json->points);

    return ['pointsOfSales'=>$poss];
}

config.yml

fos_rest:
  routing_loader:
    default_format: json
    include_format: false
  param_fetcher_listener: true
  body_listener:          true
  format_listener:        false
  view:
    view_response_listener: 'force'
  exception:
    messages:
        'Exception': true

Solution

  • Try to set the view_reponse_listener to "false" instead of "force".

    Have a look here to understand better what view_reponse_listener is: http://symfony.com/doc/current/bundles/FOSRestBundle/3-listener-support.html