2011-12-19 17 views
3

zendが初めてです。私はzendフレームワークを使ってウェブサイトを開発しました。さて、私は私のウェブサイトでgzip圧縮を設定したいと思います。これを実装するために私を賢明に案内してください。zend frameworkのWebサイトでgzip圧縮を設定するには

ありがとうございます。 カマールアロラ

答えて

4

あなたのウェブサイトに出力するgzipには2つの方法があります。あなたのウェブサーバWebserver.If使用

  1. は、サーバー上のmod_deflateをを有効にする方法の良いドキュメントのためhereを参照することができますのapacheれます。

  2. zend frameworkの使用。 this websiteからの次のコードを試してください。 ブートストラップファイルにgzip圧縮文字列を作成します。

コード:

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には、イメージ、ユーザーに送信されているサイトからだけで生のHTML/CSS/JS/XML/JSONコードを圧縮しません。

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

だから、いっぱい:

4

私はブルーノPitteliの答えを尊重あなたの先端

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

とZendのフレームワーク2(ZF2)のために作られた、私はあなたが次のように圧縮することができると思いますコードサンプルは次のようになります。

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); 
} 
関連する問題