在Magento 2中,您可以通过创建一个自定义模块来向每个页面添加自定义响应头。以下是实现此目标的步骤:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="CustomModule_CustomHeader" setup_version="1.0.0">
<sequence>
<module name="Magento_Store"/>
</sequence>
</module>
</config>
<?php
namespace CustomModule\CustomHeader\Observer;
use Magento\Framework\Event\ObserverInterface;
class CustomHeader implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$response = $observer->getEvent()->getResponse();
$response->setHeader('Custom-Header', 'Your Custom Header Value');
}
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_front_send_response_before">
<observer name="custom_header_observer" instance="CustomModule\CustomHeader\Observer\CustomHeader" />
</event>
</config>
php bin/magento module:enable CustomModule_CustomHeader
php bin/magento setup:upgrade
php bin/magento cache:clean
现在,您的自定义模块已经启用,并且每个页面都会添加一个名为"Custom-Header"的自定义响应头,其值为"Your Custom Header Value"。
请注意,以上步骤仅适用于Magento 2。对于其他版本的Magento,可能需要进行适当的调整。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云