本文介绍了检测请求是否为 ESI - Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这可能是不可能的,如果是这种情况,那么我将不得不寻找其他解决方案,所以如果无法完成,请告诉我.

This may not be possible, and if that's the case then I'll have to look for another solution, so please let me know if it can't be done.

我知道我可以获取 1=master 或 2=sub-request 的请求类型,但是有没有办法检测请求是否是 ESI 请求?

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

我的理解是 ESI 将始终是一个子请求,但是有许多不同的子请求.我需要我的响应侦听器来检测哪些肯定是 ESI 请求.

My understanding is that an ESI will always be a sub-request, but there are many different sub requests. I need my Response Listener to detect which ones are definitely ESI requests.

通常我的 ESI 请求将来自 Twig 中的 {{render_esi() }} 调用.

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

当然我可以附加一个查询参数或其他东西,但如果可能的话,我宁愿能够在没有这个的情况下进行检测.

Of course I can attach a query param or something, but I would rather be able to detect without this if it's possible.

推荐答案

我意识到这个问题太老了,从那时起您可能已经找到了解决方案,但是,最近我遇到了同样的问题,解决方法是用我自己的替换 FragmentListener 类并在 Request 对象上设置一个属性.感谢 @Johnny 提供 FragmentListener 提示.

I realise this question is super old and you've probably found a solution since then, however, recently I faced the same issue and the way around it was to replace the FragmentListener class with my own and set an attribute on the Request object. Thanks to @Johnny for the FragmentListener hint.

类似于以下内容:

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>

这篇关于检测请求是否为 ESI - Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 13:53