有没有一个文件在 Magento 中,可以输出所有的 HTML 代码?
我想要压缩所有的 HTML 输出。
我想要压缩所有的 HTML 输出。
Magento使用响应对象来发送所有输出。
所有输出都会添加到该对象中,然后调用其sendResponse
方法。
如果您想要更改输出,则可为http_response_send_before
事件设置侦听器。
<!-- in your module's config.xml -->
<http_response_send_before>
<observers>
<unique_name>
<type>singleton</type>
<class>group/observer</class>
<method>alterOutput</method>
</unique_name>
</observers>
</http_response_send_before>
然后在您的观察者中,您可以获取并设置主体内容
class Packagename_Modulename_Model_Observer
{
public function alterOutput($observer)
{
$response = $observer->getResponse();
$html = $response->getBody();
//modify html here
$response->setBody($html);
}
}
如果你感兴趣,这个事件在以下类的sendResponse
方法中被调用。
app/code/core/Mage/Core/Controller/Response/Http.php
输出本身是通过sendResponse
和outputBody
方法发送的。
lib/Zend/Controller/Response/Abstract.php
理想情况下,您应该在输出缓存之前执行代码压缩以避免频繁执行。我能想到的最好的方式是通过覆盖Mage_Page_Block_Html
并向您的新类添加以下函数来实现:
protected function _toHtml()
{
$html = parent::_toHtml();
// MINIFY CONTENTS OF $html HERE
return $html;
}
这样一来,它就可以为整个页面执行操作,返回值可以按照Magento通常的方式进行缓存。它不是针对每个块单独执行操作,这可能效率较低。
您可以始终使用ob函数在index.php中获取输出,然后对内容进行任何需要的处理。但我怀疑它是否能像启用gzip或deflate一样提升您的网站性能。