有没有办法在 Symfony 中使用命令渲染模板?
Is there a way to render a template using command in Symfony?
我有渲染问题。
这个问题发生过一次,因为我想将我从前端获得的内容放入数据库中。我显然是在名为 AppTriggerBuildCommand 的 class 中生成命令,但如图中所述,一旦代码到达此位置,问题就会出现,例如:
{{ render(controller('AppBundle:Menu:footerLarge', { 'language': language } )) }}
在 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
我已经尝试了另一个建议的两种解决方案。一旦我尝试重建 bootstrap.php 它就不会改变任何事情。
这是在
/var/www/html/iclei/vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php
另一个我们可以在这里找到:
/var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php
有人可以帮我解决这个问题吗?
您应该在命令中创建一个请求并将其添加到请求堆栈
$r = new Request();
$r->setLocale('en');
$container->set('request', $r, 'request');
$container->get('request_stack')->push($r);
我有渲染问题。
这个问题发生过一次,因为我想将我从前端获得的内容放入数据库中。我显然是在名为 AppTriggerBuildCommand 的 class 中生成命令,但如图中所述,一旦代码到达此位置,问题就会出现,例如:
{{ render(controller('AppBundle:Menu:footerLarge', { 'language': language } )) }}
在 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
我已经尝试了另一个建议的两种解决方案。一旦我尝试重建 bootstrap.php 它就不会改变任何事情。 这是在
/var/www/html/iclei/vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php
另一个我们可以在这里找到:
/var/www/html/iclei/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php
有人可以帮我解决这个问题吗?
您应该在命令中创建一个请求并将其添加到请求堆栈
$r = new Request();
$r->setLocale('en');
$container->set('request', $r, 'request');
$container->get('request_stack')->push($r);