更新 Symfony 2.4:“渲染片段只能在处理请求时完成." [英] Updating Symfony 2.4 : "Rendering a fragment can only be done when handling a Request."
问题描述
自从我迁移到 Symfony 2.4 后,我收到以下错误消息:
Since I migrated to Symfony 2.4, I'm getting the following error message :
渲染片段只能在处理请求时完成.
Rendering a fragment can only be done when handling a Request.
发生这种情况是因为,在某些页面上,我通过执行 $sf2->container->get('twig') 在由另一个旧框架处理的某些页面中使用 Twig 呈现一些模板->render("MyBundle::my-template.html.twig");
.
It's happening because, on some pages, I'm rendering some templates with Twig inside some pages which are handled by another older framework, by doing $sf2->container->get('twig')->render("MyBundle::my-template.html.twig");
.
是的,Symfony 2 没有处理这些请求是对的,但我仍然想用 Twig 渲染这些模板!为什么我不能(再)那样做?!以及如何解决这个问题?
So yes, that's right that Symfony 2 isn't handling those requests, but I still want to render those templates with Twig ! Why can't I do that (anymore) ?! And how to fix that ?
这是我在旧项目中执行以启动"SF2的代码:
Here is the code I'm executing to "boot" SF2 in my older project :
$loader = require_once __DIR__.'/../../../app/bootstrap.php.cache';
Debug::enable();
require_once __DIR__.'/../../../app/AppKernel.php';
$kernel = new AppKernel('bootstrap', true);
$kernel->loadClassCache();
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$kernel->boot();
$kernel->getContainer()->enterScope('request');
$kernel->getContainer()->set('request', $request, 'request');
$this->container = $kernel->getContainer();
顺便说一句,它可能与以下内容有关:Symfony 2.4 在 TWIG 中渲染控制器抛出渲染片段只能在处理请求时完成".例外.不过,我不想降级到 Symfony 2.3,删除 vendor
目录并没有解决我的问题.
EDIT : By the way, it might be related to that : Symfony 2.4 Rendering a controller in TWIG throws "Rendering a fragment can only be done when handling a Request." Exception .
Though, I don't want to downgrade to Symfony 2.3, and deleting the vendor
directory didn't fix my problem.
EDIT²:我发现问题是由于新的 RequestStack.
EDIT² : I found out that the problem is because of the new RequestStack.
在 HttpKernel
中,handleRaw(Request $request, $type = self::MASTER_REQUEST)
通常将请求推送到 RequestStack ($this->;requestStack->push($request);
).因此,如果我将一个公共方法 pushRequestStack($request)
添加到 HttpKernel
中,它可以工作..但是我怎么能正确地做到这一点?我没有找到任何能够从 HttpKernel
获取 $requestStack
的公共方法(因此我可以从外部推送请求)..
In HttpKernel
, handleRaw(Request $request, $type = self::MASTER_REQUEST)
normally push the request to the RequestStack ($this->requestStack->push($request);
). So if I add a public method pushRequestStack($request)
to the HttpKernel
it works.. But how could I do it properly ? I don't find any public method able to get the $requestStack
from HttpKernel
(so I can push the request externally)..
而且我不能使用普通"方法 ($kernel->handle($request)
) 因为它会抛出一些异常,例如对于不存在的路由, 或者也适用于已经被 PHP 启动的会话..
And I can't use the "normal" method ($kernel->handle($request)
) because it would throw some exceptions, for example for the route that doesn't exist, or also for the session that has already been started by PHP..
总而言之,有没有办法在不完全处理请求的情况下将我的/任何请求推送"到 requestStack 中?
推荐答案
您必须通过 Sf2 命令在request_stack"中推送新请求.
You have to push a new Request in the "request_stack" from your Sf2 command.
$this->getContainer()->get('request_stack')->push(Request::createFromGlobals());
这篇关于更新 Symfony 2.4:“渲染片段只能在处理请求时完成."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!