Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 HTML Minify Plugin

Asyraf Wahi Anuar 2 min read

Minify HTML source code for CakePHP 4. Minification enhances site performance and accessibility significantly, which immediately translates into a better user experience.

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

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:

Terminal
composer require wyrihaximus/minify-html

Load Plugin

PHP
// 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

src/View/AppView.phpPHP
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

app_local.phpPHP
'debug' => filter_var(env('DEBUG', false), FILTER_VALIDATE_BOOLEAN),

That's all. Happy coding :)

Did this tutorial help?

216 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

CakePHP 4 Soft Delete

A soft delete marks a record as no longer active or valid without deleting it from the database. Soft deletion is a widely used pattern applied for business applications.

5 min read

CakePHP

CakePHP 4 Audit Trail Using AuditStash Plugin

This plugin adds an "audit trail" to any of your Table classes in your application, allowing the web applications to record the creation, change, or deletion of any table's entities. In theory, it will store the old data before replacing it with the new data or content.

3 min read

CakePHP

CakePHP 4 Audit Log Plugin

An audit trail (audit logs) is a chronological log meant to capture any changes made to the electronic contents. The logs are used to be evidence for any changes that affected the contents.

3 min read