psr/http-server-handler
PSR-15 HTTP Server RequestHandlerInterface package. Defines the standard request handler contract used with PSR-15 middleware and PSR-7 requests/responses. Provides interfaces only, not an implementation; implementations are available separately.
This package provides only the Psr\Http\Server\RequestHandlerInterface—a tiny, framework-agnostic contract for HTTP request handlers defined in PSR-15. You’ll rarely install it directly; instead, it’s a transitive dependency of middleware-capable frameworks or libraries (e.g., Slim, Laminas Stratigility, Nyholm/psr7-server, or your own stack). To start using it:
laminas/laminas-stratigility, nyholm/psr7-server, or php-http/discovery).use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class ApiHandler implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface {
// Handle request, return response
}
}
First real-world use: Write a handler for your router or middleware stack, then register it as a delegate.
RequestHandlerInterface into middleware as $next to chain the stack:
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
$response = $handler->handle($request);
return $response->withHeader('X-Powered-By', 'MyApp');
}
RequestHandlerInterface objects as handlers—ideal for PSR-15–compliant dispatch.DelegateHandler wrappers (e.g., in Stratigility) or custom delegation to apply cross-cutting concerns (auth, logging, rate limiting).[$controller, 'index']) in a handler adapter:
class CallableHandler implements RequestHandlerInterface {
public function __construct(private callable $callable) {}
public function handle(ServerRequestInterface $request): ResponseInterface {
return ($this->callable)($request);
}
}
get_class($next)).guzzlehttp/psr7, nyholm/psr7) is compatible with psr/http-message:^1.0||^2.0. Explicitly pin versions if using PHP 8.0+ with typed return types.psr/http-server-handler manually. Its presence may conflict if your framework pins a specific PSR version (e.g., nyholm/psr7-server locks psr/http-message:^1.0). Let composers resolve it transitively.RequestHandlerInterface in middleware unit tests using getMockBuilder()->disableOriginalConstructor()->getMockForAbstractInterface() to avoid HTTP message dependencies.How can I help you explore Laravel packages today?