No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Compressing HTML before submit to client

HTML compressor. This component allows you to compress the HTML-code. If you want it is possible not to compress the contents in script and code tags. На русском


The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist zabachok/yii2-htmlcompressor "*"

or add

"zabachok/yii2-htmlcompressor": "*"

to the require section of your composer.json file.


Once the extension is installed, simply use it in your code by :

By View component

In your config file in components:

'view' => [
	'class' => '\zabachok\htmlcompressor\View',
	'compress' => YII_ENV_DEV ? false : true,
	'compressCode' => false,
	'compressScript' => false

By event

In your config file in components:

'response' => [
	'on beforeSend' => function ($event)
		/** @var $event yii\base\ViewEvent */
		$response = $event->sender;
		$compressor = new \zabachok\htmlcompressor\HtmlCompressor(false, false);
		$response->data = $compressor->make($response->data);

By behavior

If you already have custom View component, you can use behavior.

class View extends \yii\web\View
    public function behaviors()
        return [
                'class' => HtmlCompressorBehavior::className(),
                'compress' => true,
                'compressScript' => true,
                'compressCode' => true,

Withoit Yii2

Using without Yii2.

$compressor = new HtmlCompressor();
$result = $compressor->make($html);