zabachok/yii2-htmlcompressor

Smart HTML compressor

v1 2016-06-01 12:20 UTC

This package is auto-updated.

Last update: 2024-04-11 15:45:37 UTC


README

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. На русском

Installation

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.

Usage

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