erlandmuchasaj/laravel-gzip 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

erlandmuchasaj/laravel-gzip

Composer 安装命令:

composer require erlandmuchasaj/laravel-gzip

包简介

Gzip your responses.

README 文档

README

Laravel Gzip is a simple and effective way to gzip your response for a better performance.

Installation

Installing the Package

You can install the package via composer:

composer require erlandmuchasaj/laravel-gzip

Installing Brotli Extension (Optional but Recommended)

Brotli provides 15-20% better compression than Gzip and is supported by all modern browsers. To install the Brotli extension, you can use PECL:

macOS (using Homebrew):

pecl install brotli

Ubuntu/Debian:

sudo apt-get install php-brotli
# or
sudo pecl install brotli

Note

If Brotli is not available, the package will automatically fall back to Gzip compression.

Config file

Publish the configuration file using artisan.

php artisan vendor:publish --provider="ErlandMuchasaj\LaravelGzip\GzipServiceProvider"

Usage

This package has a very easy and straight-forward usage.

Laravel v11+

Just add the middleware in bootstrap/app.php, like so:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        // ...
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->append(\ErlandMuchasaj\LaravelGzip\Middleware\GzipEncodeResponse::class);
    })
    // ...

Laravel v10 and older

Just add the middleware to the $middleware array in app/Http/Kernel.php like so:

/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array<int, class-string|string>
 */
protected $middleware = [
    \ErlandMuchasaj\LaravelGzip\Middleware\GzipEncodeResponse::class,
    //...
];

Important

In a previous version, we recommended adding the middleware to the web middleware group. Now, we recommend adding to global $middleware because we want to apply gzip to all requests. Additionally, registering the middleware in the web group caused debugger to break.

Also, if you are using spatie/laravel-cookie-consent package, you should register this middleware before the \Spatie\CookieConsent\CookieConsentMiddleware::class middleware.

[!PS] To see the package working in production-like mode, use APP_ENV=production (double-check typos) and APP_DEBUG=false.

Important for Laravel v11+ / v12

If compression does not apply after changing environment/config values, clear cached config:

php artisan optimize:clear

When testing locally you can force compression:

GZIP_FORCE=true

That's it! Now your responses will be gzipped.

Troubleshooting

If you still don't see Content-Encoding: gzip or Content-Encoding: br, check:

  1. The request includes Accept-Encoding: gzip (or br).
  2. Response content is larger than GZIP_MIN_LENGTH (default is 256).
  3. Route/path is not excluded by excluded_paths.
  4. MIME type is compressible.
  5. Config cache is cleared (php artisan optimize:clear) after env changes.

Benchmark

I tested this package with a fresh installed laravel in homepage and got:

No Gzip => 72.9kb

With Gzip => 19.2kb *

Support me

I invest a lot of time and resources into creating best in class open source packages.

If you found this package helpful you can show support by clicking on the following button below and donating some amount to help me work on these projects frequently.

buy me a coffee

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please see SECURITY for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

erlandmuchasaj/laravel-gzip 适用场景与选型建议

erlandmuchasaj/laravel-gzip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 147.9k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2023 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「library」 「cms」 「response」 「laravel」 「optimize」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 erlandmuchasaj/laravel-gzip 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 147.9k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 40
  • 点击次数: 33
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 40
  • Watchers: 3
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-26