Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set gzip compression in zend framework website

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 Answers

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.

like image 197
Mehdi Fanai Avatar answered Oct 18 '25 06:10

Mehdi Fanai