Leandro Gabrielli
Leandro Gabrielli

Reputation: 315

Render view and new JSON response in symfony controller

I'm having an issue rendering a Twig view and a JSON response, I need to call the twig view and pass it a new Json response with a variable as parameter. The output error is the following "Notice: Object of class Symfony\Component\HttpFoundation\Response could not be converted to int " Here is the code

        $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'));

Upvotes: 0

Views: 5628

Answers (2)

Sentence
Sentence

Reputation: 301

Like @goto's answer, but it won't works, cause you need to use renderView instead of render:

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

Upvotes: 5

goto
goto

Reputation: 8162

The Json Response signature is

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

You are trying to give the twig to the status parameter.

You can avoid this silly error by using a good IDE

To give additional data to your response you could structure your array data returned

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

Upvotes: 1

Related Questions