softonic/laravel-middleware-request-id
Laravel middleware that assigns a unique request ID to every incoming request and response header, making it easy to trace requests through logs and distributed systems. Simple setup, configurable header name, and seamless Laravel integration.
composer require vendor/request-id (replace with actual package name).php artisan vendor:publish --tag=request-id-config.RequestId middleware. In Laravel 10/11, add it to the $middleware array in app/Http/Kernel.php. In Laravel 11/12, append it to the middleware section of bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
$middleware->append(\Vendor\RequestId\Middleware\RequestId::class);
})
X-Request-Id) and make it available for logging and downstream tracing.$request->attributes->get('request_id')) or sets a header. Use $request->header('X-Request-Id') in controllers.Log::withContext(['request_id' => $request->attributes->get('request_id')]);
Place this in a service provider or a dedicated middleware that runs after RequestId.Str::uuid(), Str::ulid(), or accept an incoming X-Request-Id header from a trusted proxy.Http::withHeaders(['X-Request-Id' => $id]).$response = $this->get('/');
$response->assertHeader('X-Request-Id');
handle() method now returns Symfony\Component\HttpFoundation\Response instead of Illuminate\Http\Response. This allows compatibility with all response types (JsonResponse, RedirectResponse, etc.). If you extend the middleware, ensure your overridden handle() method uses the same or a covariant return type.RequestId before them. In Laravel 11+ use $middleware->prepend() or adjust priority.php artisan config:cache in production. Avoid closures in config if you cache.X-Request-Id) may clash with proxies or load balancers. Customize it in config and ensure upstream services agree.php artisan route:list -v to confirm middleware is applied.How can I help you explore Laravel packages today?