Slim 3 框架 - RenderView

Slim 3 framework - RenderView

我想在我的控制器中使用 JSON 渲染视图并 return 它,就像 Symfony 中的渲染视图一样。

//use Slim\Container;
protected $ci;

    public function __construct(Container $ci)
    {
        $this->ci = $ci;
    }
public function AnalyseFacture(Request $request, Response $response,$args){
    $db    = $this->ci->dbLeak;
    $clients = self::RecuperationFacture($db);
    $data = $this->ci->view->render($response, 'envoiefacture/resultat-ajax.twig', ['liste' => $clients]);


    return $response->withJson(['data' => $data]);
}

我不明白你想要达到什么目的,但如果你想 return 在 json 响应中呈现内容,请尝试以下操作:

// ...

$this->ci->view->render($response, 'envoiefacture/resultat-ajax.twig', ['liste' => $clients]);

$data = (string)$response->getBody();

return $response->withJson(['data' => $data]);