代码之家  ›  专栏  ›  技术社区  ›  Leandro Gabrielli

symfony控制器中的呈现视图和新JSON响应

  •  0
  • Leandro Gabrielli  · 技术社区  · 7 年前

    我在呈现Twig视图和JSON响应时遇到问题,我需要调用Twig视图并向其传递一个新的JSON响应,其中包含一个变量作为参数。输出错误如下:“注意:Symfony\Component\HttpFoundation\Response类的对象无法转换为int " 这是代码

            $resultado = array('mes1_local' => $mes1_local, 'mes2_local' => $mes2_local, 'mes3_local' => $mes3_local, 'mes1_online' => $mes1_online, 'mes2_online' => $mes2_online, 'mes3_online' => $mes3_online, 'contador_pedido_local' => $contador_pedido_local, 'contador_pedido_online' => $contador_pedido_online, 'contador_total' => $contador_total, 'contador_usuarios' => $contador_usuarios);
           return new JsonResponse($resultado, $this->render('others/adminlte.html.twig'));
    
    2 回复  |  直到 7 年前
        1
  •  5
  •   Sentence cre8    5 年前

    就像“goto”的答案,但它不起作用,因为你需要使用 renderView 而不是 render :

        return new JsonResponse([
            'things' => $thingsArray,
            'anotherVariable' => $simpleVariable,
            'html' => $this->renderView('others/adminlte.html.twig', []/* template parameters goes here */),
        ]);
    
        2
  •  1
  •   goto    7 年前

    Json响应签名为

    __construct(mixed $data = null, int $status = 200, array $headers = array(), bool $json = false) 
    

    您正在尝试将细枝指定给状态参数。

    通过使用一个好的IDE,可以避免这个愚蠢的错误

    要为响应提供更多数据,可以对返回的数组数据进行结构化

       return new JsonResponse([
           'someData' => $resultado, 
           'html' => $this->render('others/adminlte.html.twig')
       );