How can I set gzip compression in zend framework website

心已入冬 提交于 2019-12-22 11:02:34

问题


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.

Thanks in advance. kamal Arora


回答1:


There are two methods to gzip output in your website.

  1. Using Webserver.If your webserver is apache you can refer here for a good documentation on how to enable mod_deflate on your server.

  2. Using zend framework. Try the following code which is from this website. Create a gzip compressed string in your bootstrap file.

Code:

try {
 $frontController = Zend_Controller_Front::getInstance();
 if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
    ob_start();
    $frontController->dispatch();
    $output = gzencode(ob_get_contents(), 9);
    ob_end_clean();
    header('Content-Encoding: gzip');
    echo $output;
} else {
    $frontController->dispatch();
}
} catch (Exeption $e) {
if (Zend_Registry::isRegistered('Zend_Log')) {
    Zend_Registry::get('Zend_Log')->err($e->getMessage());
}
$message = $e->getMessage() . "\n\n" . $e->getTraceAsString();
/* trigger event */
}

GZIP does not compress images, just the raw HTML/CSS/JS/XML/JSON code from the site being sent to the user.




回答2:


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);
}



回答3:


Honoring the answer of Bruno Pitteli, I think you can compress in the following way:

$search = array(
    '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
    '/[^\S ]+\</s',  // strip whitespaces before tags, except space
    '/(\s)+/s',       // shorten multiple whitespace sequences
    '#(?://)?<![CDATA[(.*?)(?://)?]]>#s' //leave CDATA alone
);

$replace = array(
    '>',
    '<',
    '\\1',
    "//&lt;![CDATA[n".'1'."n//]]>"
);

$content = preg_replace($search, $replace, $content);

So the full code sample now looks like:

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 = preg_replace(array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s', '#(?://)?<![CDATA[(.*?)(?://)?]]>#s'), array('>', '<', '\\1', "//&lt;![CDATA[n".'1'."n//]]>"), $content);

    if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
        header('Content-Encoding: gzip');
        $content = gzencode($content, 9);
    }

    $response->setContent($content);
}


来源:https://stackoverflow.com/questions/8561775/how-can-i-set-gzip-compression-in-zend-framework-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!