gpt4 book ai didi

php - 在 symfony 中获取容器

转载 作者:可可西里 更新时间:2023-11-01 13:50:18 25 4
gpt4 key购买 nike

我正在尝试从 Twig 模板中获取一个 block ,然后将其渲染到我的索引模板中:

{% block round1 %}
<h1> hello this is a sample for a round 1</h1>
{% endblock %}

{% block round2 %}
<h1> hello this is a sample for a round 1</h1>
{% endblock %}

然后使用

将其发送到我的 Controller
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Alvin\MainBundle\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Alvin\MainBundle\Form\Type\ResetPasswordType;


$templateContent = $this->getContainer()->get('twig')->loadTemplate('AngpaoMainBundle:Dynamic:dynamic.html.twig');
$bodydynamics = $templateContent->renderBlock('round1');

然后在我的索引模板中使用它

{{dynamic}}

但是我遇到了一个问题,symfony 说

FatalErrorException: Error: Call to undefined method 
Alvin\MainBundle\Controller\IndexController::getContainer() in /Users/alvinvaldez/Sites/alvinwebsite/src/Alvin/MainBundle/Controller/IndexController.php line 26

我不知道用什么来让容器运行。

最佳答案

Controller 没有 getContainer() 方法。

你可以像$this->container->get('twig')一样直接通过$this->container访问它

但是 Symfony 为 Controller 提供了快捷方式,你也可以使用 $this->get('twig')

关于php - 在 symfony 中获取容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21037655/

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