压缩zend框架2的html输出

Mat*_*ari 3 php output-buffering html-manipulation zend-framework2

我目前正在使用Zend Framework 2 beta for PHP 5.4.4来开发一个用于自学目的的个人webapp.

我想知道是否有可能在发送到浏览器之前拦截html输出,以便通过删除所有不必要的空格来缩小它.

我怎样才能在ZF2中实现这个结果?

mic*_*lbn 6

是的,你可以:

在Modle.php上创建一个将在完成时触发的事件

public function onBootstrap(Event $e)
{
    $app = $e->getTarget();
    $app->getEventManager()->attach('finish', array($this, 'doSomething'), 100);
}


public function doSomething ($e)
{
    $response = $e->getResponse();
    $content = $response->getBody();
    // do stuff here
    $response->setContent($content);

}
Run Code Online (Sandbox Code Playgroud)