【问题标题】:FOSRestBundle Unable to find template, can't return jsonFOSRestBundle 找不到模板,无法返回 json
【发布时间】:2015-05-30 07:55:33
【问题描述】:

我不想使用 FOSRestBundle 从 api 返回 json 响应,但 FOSRestBundle 想要返回 twig 而不是 json。

我的控制器方法如下所示

/**
 * @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

【问题讨论】:

    标签: json symfony fosrestbundle


    【解决方案1】:

    尝试将view_reponse_listener 设置为“false”而不是“force”。

    查看这里以更好地了解view_reponse_listener 是什么: http://symfony.com/doc/current/bundles/FOSRestBundle/3-listener-support.html

    【讨论】:

    • 如果我将 view_reponse_listener 设置为 false,我会得到 controller must return a response exception 但这应该由注释处理(或者,我错了?)如果我返回 JsonResponse 它没有被序列化并且我得到空对象。
    • 你必须指定一个默认的格式思想。你有一个配置问题。看看这里,在这个配置中:github.com/liuggio/symfony2-rest-api-the-best-2013-way/blob/…
    猜你喜欢
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2016-04-19
    • 2012-04-15
    • 1970-01-01
    • 2013-12-31
    • 2014-10-16
    • 1970-01-01
    相关资源
    最近更新 更多