定制 erlandmuchasaj/laravel-gzip 二次开发

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

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

erlandmuchasaj/laravel-gzip

最新稳定版本:1.2.5

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, the application env should be APP_ENV=production and the APP_DEBUG=false

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

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.

统计信息

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

GitHub 信息

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

其他信息

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