chr15k/laravel-response-compression
Laravel Response Compression
Boost your Laravel application's performance by optimizing HTTP responses with middleware for compression.
Installation
Install the package via Composer:
composer require chr15k/laravel-response-compression
Publish the configuration file:
php artisan vendor:publish --provider="Chr15k\ResponseCompression\ResponseCompressionServiceProvider"
Middleware Overview
This package provides the following middleware:
Compression Middleware
Applies Gzip or Brotli compression to HTTP responses based on client support. This reduces the size of the response payload and enhances load times.
Ideal For: Large JSON responses, static files, or data-intensive endpoints.
[!NOTE] To use Brotli effectively, ensure that the Brotli PHP extension is properly installed. https://pecl.php.net/package/brotli
[!WARNING] When using Brotli, a client-side decoding error may occur with non-secure connections, as modern browsers generally support Brotli compression only over HTTPS.
Setup
Register Middleware
Global Middleware
Apply the middleware globally to all requests:
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
...
$middleware->web(append: [
...
\Chr15k\ResponseCompression\Middleware\CompressResponse::class,
]);
})
Route Middleware
Alternatively, register it as route middleware for selective application:
use Chr15k\ResponseCompression\Middleware\CompressResponse;
Route::get('/profile', function () {
// ...
})->middleware(CompressResponse::class);
Config
/**
* Enable or disable the response compression.
*/
'enabled' => env('RESPONSE_COMPRESSION_ENABLED', true),
/**
* The compression algorithm to use. Can be either 'gzip' or 'br'.
*/
'algorithm' => env('RESPONSE_COMPRESSION_ALGORITHM', 'gzip'),
/**
* The minimum length of the response content to be compressed.
*/
'min_length' => env('RESPONSE_COMPRESSION_MIN_LENGTH', 1024),
'gzip' => [
/**
* The level of compression. Can be given as 0 for no compression up to 9
* for maximum compression. If not given, the default compression level will
* be the default compression level of the zlib library.
*
* @see https://www.php.net/manual/en/function.gzencode.php
*/
'level' => env('RESPONSE_COMPRESSION_GZIP_LEVEL', 5),
],
'br' => [
/**
* The level of compression. Can be given as 0 for no compression up to 11
* for maximum compression. If not given, the default compression level will
* be the default compression level of the brotli library.
*
* @see https://www.php.net/manual/en/function.brotli-compress.php
*/
'level' => env('RESPONSE_COMPRESSION_BROTLI_LEVEL', 5),
]
Before vs After
| Before | After |
|---|---|
Testing
composer test
Contributing
Contributions are welcome! Submit a pull request or open an issue to discuss new features or improvements.
License
The MIT License (MIT). Please see License File for more information.
Related Packages
Lightning-Fast Blade Components with `@boost` Directive
A fluent query builder for Meilisearch filter expressions in Laravel.
PHP performance tool analyser your script on time, memory usage and db query. Su...
Lightweight profiler for PHP and Laravel. Add checkpoints, measure closures, tra...
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 0.3.0 | ^8.2.0 | ^11.0| | MIT | |
| 0.2.0 | ^8.2.0 | ^11.0| | MIT | |
| 0.1.1 | ^8.3.0 | ^11.0 | MIT |