Determine if an ESI Request is Symfony2

This may not be possible, in which case I will have to look for another solution, so please let me know if this is not possible.

I know I can get a Request Type, which is either 1 = master or 2 = sub-request, but is there a way to determine if the request is an ESI request?

I understand that ESI will always be a subquery, but there are many different subprocesses. I need my response receiver to determine which ones are certainly ESI requests.

Usually my ESI requests come from a {{render_esi ()}} call in Twig.

Of course, I can attach a query parameter or something like that, but I would rather be able to detect this without it, if possible.

+4
source share
2 answers

I understand that this question is very old, and since then, you probably found a solution, however, I recently encountered the same problem, and around it the FragmentListener class was replaced with my own and set the attribute to the Request object. Thanks @Johnny for the tip to the FragmentListener .

Something like the following:

php class:

 <?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); } } 

service definition:

 <?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> 
+2
source

My first thought is to take a look at the Fragment Listener to understand how Symfony2 detects ESI requests.

You also note that "Usually my ESI requests come from a {{render_esi ()}} call in Twig." How about wrapping the render_esi function with its own, which fires an event that you could listen to. This will be practically what the Fragment Listener does.

Hope this helps. I am sure there are many other ways to do this.

0
source

Source: https://habr.com/ru/post/1484306/


All Articles