CakePHP 4 HTML Minify Plugin
Asyraf Wahi Anuar - December 02, 2021Estimated reading time: 59 seconds

The practice of reducing the amount of code and markup in your web pages and script files is known as minification. It is one of the most used strategies for reducing website load times and bandwidth utilisation. Minification enhances site performance and accessibility significantly, which immediately translates into a better user experience. It's also useful for consumers that reach your website via a restricted data plan and want to reduce their bandwidth use while surfing the web. To minify the CakePHP web application codes, HTML Minify Plugin can be used.
Repository URL: https://github.com/WyriHaximus/MinifyHtml
Installation
Download the plugin using composer:
composer require wyrihaximus/minify-html
Load Plugin
// In src/Application.php
use Cake\Http\BaseApplication;
use WyriHaximus\MinifyHtml\Plugin as MinifyHtmlPlugin;
class Application extends BaseApplication {
public function bootstrap()
{
parent::bootstrap();
// Load the minify html plugin by class name
$this->addPlugin(MinifyHtmlPlugin::class);
}
}
Load Helper
Load helper in .../src/View/AppView.php
public function initialize(): void
{
$this->loadHelper('WyriHaximus/MinifyHtml.MinifyHtml');
}
If the debug mode is 'true', it will not minify the HTML source code. You can disable debug mode from ...config/app_local.php
'debug' => filter_var(env('DEBUG', false), FILTER_VALIDATE_BOOLEAN),
That's all. Happy coding :)






