定制 ctw/ctw-middleware-htmlminifier 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ctw/ctw-middleware-htmlminifier

Composer 安装命令:

composer require ctw/ctw-middleware-htmlminifier

包简介

This PSR-15 middleware formats, fixes and beautifies the HTML in the Response body using a variety of adapters.

README 文档

README

Latest Stable Version GitHub Actions Scrutinizer Build Scrutinizer Quality Code Coverage

PSR-15 middleware that minifies HTML responses using pluggable adapters, reducing bandwidth and improving page load times.

Introduction

Why This Library Exists

HTML generated by template engines often contains excessive whitespace, comments, and formatting that increases page size without benefiting end users. Minifying HTML at the response level provides several advantages:

  • Reduced bandwidth: Smaller responses transfer faster, especially on mobile networks
  • Lower hosting costs: Less data transferred means reduced bandwidth bills
  • Improved performance: Smaller payloads parse faster in browsers
  • Production optimization: Keep source templates readable while serving optimized output

This middleware intercepts HTML responses and minifies them transparently, with multiple adapter options to balance speed versus compression ratio.

Problems This Library Solves

  1. Bloated HTML: Template engines produce readable but unnecessarily large output
  2. Manual optimization: Without automation, developers must manually minify HTML
  3. Inconsistent minification: Different approaches across a codebase lead to varying output quality
  4. Build complexity: Template-level minification complicates the build process
  5. Debugging difficulty: Aggressive minification can break HTML; adapters provide different safety levels

Where to Use This Library

  • Production environments: Reduce HTML size for faster page loads
  • High-traffic sites: Minimize bandwidth costs at scale
  • Mobile-first applications: Optimize for slower network connections
  • Content-heavy pages: Blog posts, documentation, and marketing pages benefit most
  • API responses: HTML fragments returned by AJAX endpoints

Design Goals

  1. Pluggable adapters: Choose the right minification strategy for your needs
  2. Safe defaults: Conservative minification that won't break valid HTML
  3. Transparent operation: Works automatically on HTML responses only
  4. Statistics tracking: Appends an HTML comment showing compression ratio
  5. Zero template changes: Minifies at response level, not template level

Requirements

  • PHP 8.3 or higher
  • ext-tidy
  • ctw/ctw-middleware ^4.0

Installation

Install by adding the package as a Composer requirement:

composer require ctw/ctw-middleware-htmlminifier

Usage Examples

Basic Pipeline Registration (Mezzio)

use Ctw\Middleware\HtmlMinifierMiddleware\HtmlMinifierMiddleware;

// In config/pipeline.php or similar
$app->pipe(HtmlMinifierMiddleware::class);

ConfigProvider Registration

// config/config.php
return [
    // ...
    \Ctw\Middleware\HtmlMinifierMiddleware\ConfigProvider::class,
];

Available Adapters

Adapter Description Speed Compression
SimpleAdapter Regex-based whitespace removal Fast Moderate
TidyAdapter Uses PHP's Tidy extension for safe minification Medium Good
WyriHaximusAdapter Full-featured HTML compressor (requires additional package) Slower Best

Using SimpleAdapter

The SimpleAdapter uses regular expressions for fast, lightweight minification:

use Ctw\Middleware\HtmlMinifierMiddleware\Adapter\SimpleAdapter\SimpleAdapter;

Performs:

  • Removes multi-line whitespace
  • Removes HTML comments
  • Collapses multiple spaces
  • Strips whitespace around tags

Using TidyAdapter

The TidyAdapter leverages PHP's Tidy extension for standards-compliant minification:

use Ctw\Middleware\HtmlMinifierMiddleware\Adapter\TidyAdapter\TidyAdapter;

Features:

  • Fixes malformed HTML
  • Validates HTML5 doctype
  • Configurable output formatting
  • Safe for production use

Using WyriHaximusAdapter

For maximum compression, install the optional dependency:

composer require wyrihaximus/html-compress
use Ctw\Middleware\HtmlMinifierMiddleware\Adapter\WyriHaximusAdapter\WyriHaximusAdapter;

Output Statistics

The middleware appends an HTML comment showing compression statistics:

<!-- html: in 15420 b | out 12336 b | diff 20.0000 % -->
Field Description
in Original HTML size in bytes
out Minified HTML size in bytes
diff Percentage reduction achieved

Selective Processing

The middleware automatically:

  • Only processes responses with Content-Type: text/html or application/xhtml
  • Passes through empty responses unchanged
  • Skips non-HTML responses (JSON, images, etc.)

ctw/ctw-middleware-htmlminifier 适用场景与选型建议

ctw/ctw-middleware-htmlminifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 158 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ctw/ctw-middleware-htmlminifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ctw/ctw-middleware-htmlminifier 我们能提供哪些服务?
定制开发 / 二次开发

基于 ctw/ctw-middleware-htmlminifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 158
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-03-20