如何在zend Framework网站中设置gzip压缩
问题描述:
我是zend的新手.我已经使用zend框架开发了一个网站.现在,我想在我的网站中设置gzip压缩.您能指导我明智地实现这一点吗?
I am new to zend. I have developed a website using zend framework. Now, I want to set gzip compression in my website. Would you please guide me step wise to implement this.
先谢谢了. 卡马尔·阿罗拉(Kamal Arora)
Thanks in advance. kamal Arora
答
我用您的技巧为zend framework 2(zf2)制作了
I made for zend framework 2 (zf2) with your tip
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach("finish", array($this, "compressOutput"), 100);
}
public function compressOutput($e)
{
$response = $e->getResponse();
$content = $response->getBody();
$content = str_replace(" ", " ", str_replace("\n", " ", str_replace("\r", " ", str_replace("\t", " ", $content))));
if(@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)
{
header('Content-Encoding: gzip');
$content = gzencode($content, 9);
}
$response->setContent($content);
}