在zend框架,我想删除空白和新行在我的html响应


In zend framework i want to remove white space and new line in my html response

我想压缩我的html/js代码,当我得到响应从前端控制器在zend或之前的内容发送到客户端。

当我在view source中查看此代码时,它显示所有带有空格的代码,而不像在Google中。

有没有办法做到这一点

你可以创建一个在dispatchLoopShutdown期间运行的插件,从那里你可以获得响应的主体,操作它,然后在最小化它后设置主体。

下面是一个例子:

<?php
class Application_Plugin_ShrinkSource extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopShutdown()
    {
        // get the output that will be sent to the client
        $body = $this->getResponse()->getBody();
        // remove extra whitespace, and other cleanup
        $body = someFunctionToRemoveWhitespace($body)
        // set the modified content back to the body
        $this->getResponse()->setBody($body);
    }
}

在你的引导中,你可以像

那样注册插件
Zend_Controller_Front::getInstance()
    ->registerPlugin(new Application_Plugin_ShrinkSource());