仅针对Magento 2.1上的特定页面设置/覆盖x-frame选项

仅针对Magento 2.1上的特定页面设置/覆盖x-frame选项,magento,iframe,http-headers,magento2,x-frame-options,Magento,Iframe,Http Headers,Magento2,X Frame Options,Magento 2.1将所有前端页面的X-Frame-Option标题设置为同一格式。它允许我们在app/etc/env.php配置中对其进行更改,但前端的所有页面都会更改 在我的情况下,我想改变它的一个特定的页面。在控制器上设置此选项不起作用,因为Magento稍后会在插件调用中覆盖它 我用Varnish模块设置其标题的相同方式使用插件实现了这一点 但是,因为我只想允许它用于一个页面,所以我对这种方法不满意,因为它会在前端的每个响应上增加一些开销。这可能是正确的Magento方式,但我是Mag

Magento 2.1将所有前端页面的X-Frame-Option标题设置为同一格式。它允许我们在app/etc/env.php配置中对其进行更改,但前端的所有页面都会更改

在我的情况下,我想改变它的一个特定的页面。在控制器上设置此选项不起作用,因为Magento稍后会在插件调用中覆盖它

我用Varnish模块设置其标题的相同方式使用插件实现了这一点

但是,因为我只想允许它用于一个页面,所以我对这种方法不满意,因为它会在前端的每个响应上增加一些开销。这可能是正确的Magento方式,但我是Magento的新手,所以我不确定

我在这方面做的不多,这不会对请求产生很大影响,因为我没有在其他页面上使用DB或任何东西。但我想知道是否有人找到了更好的方法

<?php
namespace Nei\MyModule\Plugin;
use Nei\MyModule\Model\Config;
use Magento\Framework\App\PageCache\NotCacheableInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\Http;
/**
 * HTTP response plugin for frontend.
 */
class AllowOriginHeaderForTrackingPlugin
{
    /**
     * @var Config
     */
    private $config;
    /**
     * @var RequestInterface
     */
    private $request;
    public function __construct(
        Config $config,
        RequestInterface $request
    ) {
        $this->config = $config;
        $this->request = $request;
    }
    /**
     * Magento by default override X-Frame-Option header after Dispatcher.
     * So setting this header on controller will be overridden
     *
     * With this Plugin we override Magento's override for X-Frame-Option
     *
     * @param Http $subject
     * @return void
     */
    public function beforeSendResponse(Http $subject)
    {
        if ($subject instanceof NotCacheableInterface) {
            return;
        }
        // Only for tracking action
        $frontName = strtolower($this->request->getFrontName());
        $actionName = strtolower($this->request->getActionName());

        // @TODO: update the frontend and action name here
        if ($frontName === 'something' && $actionName === 'anything') {
            $xFrameOption = $this->config->getXFrameOption();
            if (!empty($xFrameOption) && $this->config->isTrackingEnabled()) {
                $subject->setXFrameOptions('ALLOW-FROM '.$xFrameOption);
            }
        }
    }
}

由于我们仅在Magento 2.2.x站点中覆盖特定页面的标题,您是否找到了其他方法?由于我们仅在Magento 2.2.x站点中覆盖特定页面的标题,您是否找到了其他方法?
<!-- Nei_MyModule/etc/frontend/di.xml -->
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <type name="Magento\Framework\App\Response\Http">
        <plugin name="nei-mymodule-allow-origin" type="Nei\MyModule\Plugin\AllowOriginHeaderForTrackingPlugin"/>
  </type>
</config>