PSR-15 compliant middleware to compress html responses.
composer require steffenbrand/html-compress-middleware
Add the middleware to your config/pipeline.php
/**
* Setup middleware pipeline:
*/
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
// The error handler should be the first (most outer) middleware to catch
// all Exceptions.
$app->pipe(ErrorHandler::class);
$app->pipe(HtmlCompressMiddleware::class);
...
}
Add the middleware to your config/autoload/dependencies.global.php
Or use it as a routed middleware, if you don't want the middleware to be invoked on certain routes.
return [
// Provides application-wide services.
// We recommend using fully-qualified class names whenever possible as
// service names.
'dependencies' => [
...
'factories' => [
// Fully\Qualified\ClassName::class => Fully\Qualified\FactoryName::class,
\SteffenBrand\HtmlCompressMiddleware\HtmlCompressMiddleware::class => \SteffenBrand\HtmlCompressMiddleware\HtmlCompressMiddlewareFactory::class,
],
],
];
To enable compression disable the development mode.
HTML compression is only meant for production.
composer development-disable
To disable compression enable the development mode.
HTML compression will not run in development mode.
composer development-enable
Or provide your own development-mode settings by editing the file config/autoload/development.local.php.dist