我意识到这个问题已经过时了,从那时起您可能已经找到了解决方案,但是,最近我遇到了同样的问题,解决方法是FragmentListener
用我自己的类替换该类并在Request
对象上设置一个属性。感谢@Johnny 的FragmentListener
提示。
类似于以下内容:
php类:
<?php
namespace Your\Namespace\Here;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\EventListener\FragmentListener as SymfonyFragmentListener;
class FragmentListener extends SymfonyFragmentListener
{
private $signer;
private $fragmentPath;
/**
* {@inheritdoc}
*/
public function __construct(UriSigner $signer, $fragmentPath = '/_fragment')
{
parent::__construct($signer, $fragmentPath);
$this->signer = $signer;
$this->fragmentPath = $fragmentPath;
}
/**
* {@inheritdoc}
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (
$request->attributes->has('_controller')
|| $this->fragmentPath !== rawurldecode($request->getPathInfo())
) {
return;
}
$event->getRequest()->attributes->set('esi', true);
parent::onKernelRequest($event);
}
}
服务定义:
<?xml version="1.0" ?>
<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<parameters>
<parameter key="fragment.listener.class">Your\Namespace\Here\FragmentListener</parameter>
</parameters>
</container>