psr/http-server-middleware
PSR-15 HTTP server middleware interfaces for PHP. Provides the MiddlewareInterface specification used by request handlers and middleware stacks; no implementation included. Install via psr/http-server-middleware and use with compatible frameworks or libraries.
This package provides only the MiddlewareInterface (PSR-15) definition—no implementation, no runtime logic. Start here if you’re building or integrating middleware into a PSR-15-compliant HTTP server (e.g., Expressive/Laminas, Symfony with middleware bridge, custom stack). First step: require it via Composer to get the interface:
composer require psr/http-server-middleware
Then implement the interface in your middleware classes:
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class ExampleMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Pre-processing (e.g., logging, auth check)
$response = $handler->handle($request);
// Post-processing (e.g., add headers, modify body)
return $response->withHeader('X-Processed-By', 'ExampleMiddleware');
}
}
Most modern frameworks (Laravel via laravel/framework + middleware adapter, Symfony with symfony/http-kernel + middleware bridge, Laminas Expressive) already consume this interface indirectly.
Middleware contract, but adapters like laravel/framework's Pipeline or third-party bridges (eloquent/php-psr15-bridge) can translate between them.AuthenticationMiddleware, RateLimitingMiddleware, LocaleMiddleware—all conforming to MiddlewareInterface. Chaining is handled by the framework’s request handler stack, not this package.RequestHandlerInterface to delegate processing cleanly. In tests, mock handlers to isolate middleware logic.psr/http-message 1.x and 2.x (1.0.2 added 2.x support).relax/psr15-middleware, Nyholm/psr7, php-http/discovery, or a full framework) that consumes the interface.psr/http-server-middleware-implementation providers on Packagist for actual implementations.app/Http/Kernel.php, but note Laravel uses its own abstraction—PSR-15 is only used internally via adapter layers or if you explicitly integrate it.symfony/psr-http-message-bridge to convert Symfony Request/Response to PSR-7 and feed into a PSR-15 middleware stack.phpspec or phpunit mocks for RequestHandlerInterface. A common anti-pattern is calling $handler->handle() more than once; assert exactly once to avoid bugs.process(). Keep middleware pure and testable.How can I help you explore Laravel packages today?