简体   繁体   English

有没有办法在 Symfony 中使用命令来渲染模板?

[英]Is there a way to render a template using command in Symfony?

I have a rendering problem.我有一个渲染问题。 在此处输入图片说明

This problem occurs once as I want to put inside a database the content which I have from the frontend.这个问题发生一次,因为我想将前端的内容放入数据库中。 I am obviously generating the command in a class called AppTriggerBuildCommand, but as described in the image the problem occurs once the code reaches this for example:我显然是在一个名为 AppTriggerBuildCommand 的类中生成命令,但如图像中所述,一旦代码达到此问题就会出现问题,例如:

{{ render(controller('AppBundle:Menu:footerLarge', { 'language': language }  )) }}

inside the page.html.twig .在 page.html.twig 里面。

  [Twig_Error_Runtime]                                                                                                                 
  An exception has been thrown during the rendering of a template ("Rendering a fragment can only be done when handling a Request.").  


Exception trace:
 () at /var/www/html/iclei/src/AppBundle/Resources/views/page.html.twig:23
 Twig_Template->displayWithErrorHandling() at /var/www/html/iclei/vendor/twig/twig/lib/Twig/Template.php:366
 Twig_Template->display() at /var/www/html/iclei/vendor/twig/twig/lib/Twig/Template.php:374
 Twig_Template->render() at /var/www/html/iclei/vendor/twig/twig/lib/Twig/Environment.php:289
 Twig_Environment->render() at /var/www/html/iclei/src/AppBundle/Command/AppTriggerBuildCommand.php:69
 AppBundle\Command\AppTriggerBuildCommand->pageAction() at /var/www/html/iclei/src/AppBundle/Command/AppTriggerBuildCommand.php:38
 AppBundle\Command\AppTriggerBuildCommand->execute() at /var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:242
 Symfony\Component\Console\Command\Command->run() at /var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:861
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:193
 Symfony\Component\Console\Application->doRun() at /var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:83
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:117
 Symfony\Component\Console\Application->run() at /var/www/html/iclei/app/console:27

I have tried both of the solutions suggested in the other.我已经尝试了另一个中建议的两种解决方案。 Once I have tried to rebuild bootstrap.php it just doesn't change a thing.一旦我尝试重建 bootstrap.php,它就不会改变任何事情。 This is inside the这是里面的

/var/www/html/iclei/vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php

And the other we can find it in here:另一个我们可以在这里找到它:

/var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php

Could someone help me to get over this ?有人能帮我解决这个问题吗?

You should create a request in your command and add it to the request stack您应该在命令中创建一个请求并将其添加到请求堆栈中

$r = new Request();
$r->setLocale('en');
$container->set('request', $r, 'request');
$container->get('request_stack')->push($r);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM