I tested Symfony 2.2, FOSRest Bundle (using JMS Serializer) and Doctrine ODM using MongoDB.
After many hours of trying to figure out how to properly configure the FOSRest package, I still have problems: I have a very simple route that returns a list of products and prices. Whenever I request the HTML format, I get the correct answer, but if I request any other format (JSON, XML), I get the error message:
[{"message": "Resources are not supported in serialized data. Path: Monolog\\Handler\\StreamHandler -> Symfony\\Bridge\\Monolog\\Logger -> Doctrine\\Bundle\\MongoDBBundle\\Logger\\Logger -> Doctrine\\Bundle\\MongoDBBundle\\Logger\\AggregateLogger -> Doctrine\\ODM\\MongoDB\\Configuration -> Doctrine\\MongoDB\\Connection -> Doctrine\\ODM\\MongoDB\\LoggableCursor", "class": "JMS\\Serializer\\Exception\\RuntimeException",...
you can see the full message here
My current setup is very simple: I created one route to the controller, which returns a list of products and price (I followed in this example to create a product document).
This is the route:
rest_product: type: rest resource: Onema\RestApiBundle\Controller\ProductController
This is the controller:
<?php namespace Onema\RestApiBundle\Controller; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use FOS\RestBundle\Controller\FOSRestController; use FOS\RestBundle\Routing\ClassResourceInterface; use FOS\Rest\Util\Codes; use JMS\Serializer\SerializationContext; use Onema\RestApiBundle\Document\Product; class ProductController extends FOSRestController implements ClassResourceInterface { public function getAction() { $dm = $this->get('doctrine_mongodb')->getManager(); $products = $dm->getRepository('RestApiBundle:Product')->findAll(); if(!$products) { throw $this->createNotFoundException('No product found.'); } $data = array('documents' => $products); $view = $this->view($data, 200); $view->setTemplate("RestApiBundle:Product:get.html.twig"); return $this->handleView($view); } }
This is the view called from the Resources / Product / get.html.twig controller:
<ul> {% for document in documents %} <li> {{ document.name }}<br /> {{ document.price }} </li> {% endfor %} </ul>
Any ideas why this will work correctly for one format, but not for others? Anything else I need to set up?
UPDATE: These are the configuration values ββthat I used. At the end of app / config / config.yml, I had the following:
sensio_framework_extra: view: { annotations: false } router: { annotations: true } fos_rest: param_fetcher_listener: true body_listener: true format_listener: true view: formats: json: true failed_validation: HTTP_BAD_REQUEST default_engine: twig view_response_listener: 'force'
WORKAROUND:
After doing some more research, I came across another error that led me to these questions and answered:
stack overflow
Once I got rid of Doctrine\ODM\MongoDB\LoggableCursor , adding each result to an array like this:
$productsQ = $dm->getRepository('RestApiBundle:Product')->findAll(); foreach ($productsQ as $product) { $products[] = $product; } return $products;
I started to get the results in the correct format. This is a kind of lame solution and still hopes to find a better answer to this question.