symfony中的getContainer

Aoi*_*Aoi 5 php symfony

嘿家伙我试图从树枝模板中获取一个块,然后将它渲染到我的索引模板:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

然后使用它到我的控制器

  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');
Run Code Online (Sandbox Code Playgroud)

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

{{dynamic}}
Run Code Online (Sandbox Code Playgroud)

但后来我有一个问题,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
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用容器运行.请帮帮我们

顺便说一句:symfony中的新手级别

tnx在先进!

xda*_*azz 8

没有getContainer()控制器的方法.

您可以$this->container直接访问它$this->container->get('twig')

但是Symfony为控制器提供了快捷方法,你也可以使用它$this->get('twig').