更新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')->render("MyBundle::my-template.html.twig");
在某些页面内用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.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² : 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屋!