我一直在试用 Symfony 2.2、FOSRest Bundle(使用 JMS Serializer)和使用 MongoDB 的 Doctrine ODM。
在尝试找出如何正确设置 FOSRest Bundle 的许多小时后,我仍然遇到了一些麻烦:我有一个非常简单的路线,可以返回产品和价格列表。 每当我请求 HTML 格式时,我都会得到正确的响应,但如果我请求任何其他格式(JSON、XML),我会收到错误消息:
[{"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",...
您可以看到完整的错误信息here
我当前的设置非常简单:我创建了一个到 Controller 的单一路由,该 Controller 返回产品列表和价格(我按照 this example 创建产品文档)。
这是路线:
rest_product:
type: rest
resource: Onema\RestApiBundle\Controller\ProductController
这是 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);
}
}
这是从 Controller Resources/Product/get.html.twig 调用的 View :
<ul>
{% for document in documents %}
<li>
{{ document.name }}<br />
{{ document.price }}
</li>
{% endfor %}
</ul>
知道为什么这对一种格式有效但对其他格式无效吗?我还应该设置什么吗?
更新: 这是我一直在使用的配置值。 在 app/config/config.yml 的最后我有这个:
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'
解决方法:
做了更多的研究,我遇到了另一个错误,导致我提出这个问题和答案:
https://stackoverflow.com/a/14030646/155248
一旦我通过将每个结果添加到数组中来摆脱 Doctrine\ODM\MongoDB\LoggableCursor
,如下所示:
$productsQ = $dm->getRepository('RestApiBundle:Product')->findAll();
foreach ($productsQ as $product) {
$products[] = $product;
}
return $products;
我开始以正确的格式获取结果。这是一种蹩脚的解决方案,仍然希望找到更好的答案来解决这个问题。
最佳答案
如果你想获得 RestApiBundle:Product 文档的集合,你必须在从存储库调用 find 方法或从查询构建器调用 getQuery 方法后调用方法“toArray”
/**
* @Route("/products.{_format}", defaults={"_format" = "json"})
* @REST\View()
*/
public function getProductsAction($_format){
$products = $this->get('doctrine_mongodb')->getManager()
->getRepository('RestApiBundle:Product')
->findAll()->toArray();
return $products;
}
您也可以调用 array_values($products) 以正确序列化排除策略
关于php - FOSRestBundle 和 JMS Serializer,获取 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16173429/