Search by

sadekd / laravel-request-id

sadekd

Laravel Request ID

Package info

github.com/sadekd/laravel-request-id

pkg:composer/sadekd/laravel-request-id

Statistics

Installs: 15 698

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v1.0.1 2023-09-12 14:15 UTC

This package is auto-updated.

Last update: 2026-10-06 18:14:47 UTC


README

This Laravel package to help you add X-Request-ID feature into your Laravel app

The HTTP X-Request-ID request header is an optional and unofficial HTTP header, used to trace individual HTTP requests from the client to the server and back again. It allows the client and server to correlate each HTTP request.

Installation

composer require sadekd/laravel-request-id

Config

php artisan vendor:publish --provider="SadekD\LaravelRequestId\LaravelRequestIdServiceProvider"

Configuration is commented in the config file

Usage

Register the middleware as high in the stack as possible (higher is better — on a later failure you still have the XRID in logs, the response, etc.). The package registers a request-id middleware alias automatically.

Laravel 11+ (bootstrap/app.php)

->withMiddleware(function (Middleware $middleware) {
    $middleware->prepend(\SadekD\LaravelRequestId\Http\Middleware\RequestId::class);
    // or: $middleware->prepend('request-id');
})

Laravel 10 (app/Http/Kernel.php)

protected $middleware = [
    \SadekD\LaravelRequestId\Http\Middleware\RequestId::class,
    // or just 'request-id',
];

Reading the ID

use SadekD\LaravelRequestId\Facades\RequestId;

RequestId::getId();   // current request's ID
app('request-id')->getId();

Config options

Published to config/request-id.php:

Key Default Description
enabled true Master switch.
key X-Request-ID Header name (also used as the log-context / config key).
accept_request_headers false Trust an inbound X-Request-ID header. Validated: non-empty, ≤128 chars, [A-Za-z0-9-_.] only.
request_headers true Write the ID onto the incoming request headers.
config false Store the ID in Laravel config under key.
log_context true Add ID + hostname + URL to log context (Context::add on L11+, Log::withContext otherwise).
response_headers true Echo the ID on the response.
generator ulid ulid or uuid (via sadekd/id-generator's IdGenerator).
enable_telescope_tags true Tag Telescope entries with the ID when Telescope is installed.

License

The MIT License (MIT). Please see License File for more information.