middlewares/request-handler
PSR-15/PSR-7 request handler middleware for PHP. Build a flexible pipeline that routes an incoming ServerRequest to your handler stack, with clean delegation and composable middlewares—ideal for frameworks or custom apps needing standard request processing.
Installation
composer require middlewares/request-handler
Add to composer.json under require-dev if using for testing.
Basic Usage
Register the middleware in app/Http/Kernel.php:
protected $middleware = [
\Middlewares\RequestHandler::class,
];
First Use Case
Define a handler class (e.g., app/Handlers/ValidateRequest.php):
namespace App\Handlers;
use Middlewares\RequestHandler\HandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class ValidateRequest implements HandlerInterface
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
if (!$request->getAttribute('valid')) {
return $response->withStatus(400);
}
return $response;
}
}
Bind the handler in AppServiceProvider:
public function boot()
{
$this->app->bind(\Middlewares\RequestHandler\HandlerInterface::class, \App\Handlers\ValidateRequest::class);
}
Chaining Handlers
Use middleware groups or stack handlers via HandlerInterface:
// app/Handlers/ChainHandlers.php
class ChainHandlers implements HandlerInterface
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
$handler = $this->app->make(HandlerInterface::class);
return $handler($request, $response);
}
}
Conditional Execution Skip handlers based on request attributes or routes:
if (!$request->getAttribute('skip_validation')) {
return $handler($request, $response);
}
Dependency Injection Inject services into handlers via Laravel’s container:
class AuthHandler implements HandlerInterface
{
protected $auth;
public function __construct(AuthManager $auth)
{
$this->auth = $auth;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
if (!$this->auth->check()) {
return $response->withStatus(401);
}
return $response;
}
}
Route::middleware(RequestHandler::class)->group(function () {
// Routes here
});
$this->app->instance(HandlerInterface::class, MockHandler::class);
Circular Dependencies Avoid binding handlers that depend on other handlers in the same stack. Use explicit service binding instead.
Request/Response Mutations
Handlers may unintentionally modify $request or $response. Document expectations clearly:
// Example: Do NOT modify $request->getParsedBody() if downstream handlers rely on it.
Middleware Order
The package executes handlers after Laravel’s middleware. Reorder in Kernel.php if needed:
protected $middlewareGroups = [
'web' => [
// Custom middleware first...
\Middlewares\RequestHandler::class,
],
];
PSR-15 Compliance
Ensure responses adhere to PSR-7 (e.g., withStatus() over setStatusCode()).
try {
$handler($request, $response);
} catch (\Throwable $e) {
report($e); // Log via Laravel's error handler
}
next($request) pattern if chaining.$this->app->bind(
\Middlewares\RequestHandler\HandlerResolver::class,
\App\Handlers\CustomResolver::class
);
#[Handler(priority: 10)] via attributes).Laravel\Bus\Queueable for background processing:
class AsyncHandler implements HandlerInterface, ShouldQueue
{
// ...
}
How can I help you explore Laravel packages today?