如何在树枝扩展中呈现控制器操作?

Two*_*oez 3 symfony

我有一个twig扩展,它有一个方法,使用来自不同控制器的另一个方法通过依赖jsonResponse呈现json输出.

如何在枝条扩展中渲染控制器?

下面的代码似乎不起作用,因为render()需要一个视图文件而不是一个控制器.我现在引用一个控制器.

class AreaExtension extends \Twig_Extension {

    public function add()
    {

        $outputJson = $this->container->get('templating')->render(new ControllerReference('CMSCoreBundle:Area:index'));
    }
}
Run Code Online (Sandbox Code Playgroud)

Zso*_*ösi 5

$ref = new ControllerReference('CMSCoreBundle:Area:index');
$this->handler->render( $ref, 'inline', $options );
Run Code Online (Sandbox Code Playgroud)

哪里$this->handlerfragment.handler服务.

在你的情况下:

$outputJson = $this->container->get('fragment.handler')->render(new ControllerReference('CMSCoreBundle:Area:index'));
Run Code Online (Sandbox Code Playgroud)

您可以在此symfony twig扩展中找到完整的示例,请参阅: https://github.com/symfony/symfony/blob/4.1/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php#L28https:// github.com/symfony/symfony/blob/4.1/src/Symfony/Bridge/Twig/Extension/HttpKernelRuntime.php#L41