gpt4 book ai didi

Symfony 渲染 Controller 异常模板 ("")

转载 作者:行者123 更新时间:2023-12-03 18:23:18 24 4
gpt4 key购买 nike

我试图在 Twig 中渲染一个 Controller 。我遵循文档“嵌入其他 Controller ”。

我经常使用方法 render(controller()) 得到一个异常(exception):渲染模板("") 时抛出异常

在我的 template.html.twig 中

{{ render(controller('MyBundle:User:logo')) }}

在我的 Controller 中

class UserController extends Controller {
public function logoAction()
{
return $this->render('MyBundle:User:logo.html.twig', []);
}
}

如果我在我的模板中使用它效果很好

{% render url('logo') %}

随着路线

logo:
pattern: /logo
defaults: { _controller: MyBundle:User:logo }

但我想使用 render(controller())

我做错了什么?

编辑,添加更多关于异常的信息:

An exception has been thrown during the rendering of a template ("") in MyBundle:My/Dashboard:dashboard.html.twig at line 51.

500 Internal Server Error - Twig_Error_Runtime 1 linked Exception: ResourceNotFoundException »

[2/2] Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("") in MyBundle:My/Dashboard:dashboard.html.twig at line 51. -

in app/cache/dev/classes.php at line 7378 - } catch (Twig_Error $e) { throw $e; } catch (Exception $e) { throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, $template->getTemplateName(), $e); } } elseif (false !== $parent = $this->getParent($context)) { $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks), false); at Twig_Template
->displayBlock ('body', array('preset' => array('date' => '2015-03-31'),... 1/2] ResourceNotFoundException: -

in app/cache/dev/appDevUrlMatcher.php at line 632 -
}
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
} } at appDevUrlMatcher ->match ('/_fragment') in app/cache/dev/classes.php at line 3278 + at Router ->match ('/_fragment')

Edit2 dashboard.html.twig

{% block body %}
<div class="panel panel-default">
<div class="panel-heading">
<span class="glyphicon glyphicon-random" aria-hidden="true"></span>
<b>{{ "Request"|trans }}</b>
</div>
<div class="panel-body">
<form role="form" action="{{ path('dashboard', {'idDbd': idDbd}) }}">
<div class="form-inline">
{{ render(controller('MyBundle:User:logo')) }}
</div>
{% include 'MyBundle:My/Request:request_edit.html.twig' %}
</form>
</div>
</div>
(...)

最佳答案

为了能够对子请求使用 controller() 表示法,您需要启用能够处理此类子请求的特殊 FragmentListener。

您需要在 config.yml 文件中包含它的配置。这是在新的 Symfony 项目中默认完成的:https://github.com/symfony/symfony-standard/blob/7311e7065bea8986f8eb2e95ab9dd4a55ce95ae2/app/config/config.yml#L31查看你自己的项目中是否有这一行

关于Symfony 渲染 Controller 异常模板 (""),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30045841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com