PHPackages                             php-middleware/request-id - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. php-middleware/request-id

ActiveLibrary[HTTP &amp; Networking](/categories/http)

php-middleware/request-id
=========================

Request Id middleware with PSR-7

4.1.0(5y ago)36295.5k↓21.7%4[1 issues](https://github.com/php-middleware/request-id/issues)[1 PRs](https://github.com/php-middleware/request-id/pulls)2MITPHPPHP ^7.1 | ^8.0CI passing

Since Sep 2Pushed 3mo ago4 watchersCompare

[ Source](https://github.com/php-middleware/request-id)[ Packagist](https://packagist.org/packages/php-middleware/request-id)[ RSS](/packages/php-middleware-request-id/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (6)Versions (19)Used By (2)

Request ID middleware for PHP
=============================

[](#request-id-middleware-for-php)

[![Build Status](https://camo.githubusercontent.com/705046a6b0943eaac6cbbe0b3199a6deb84812376e7571a11c3137ac2b79f799/68747470733a2f2f7472617669732d63692e6f72672f7068702d6d6964646c65776172652f726571756573742d69642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/php-middleware/request-id)

PSR-7 Request ID middleware

This middleware provide framework-agnostic possibility to generate and add to request/response's header Request ID (Correlation ID).

Installation
------------

[](#installation)

```
composer require php-middleware/request-id
```

Usage
-----

[](#usage)

This middleware require in contructor `PhpMiddleware\RequestId\RequestIdProviderFactoryInterface` implementation which must create a new `RequestIdProviderInterface` object. We provide `RequestIdProvider` default implementation.

```
$generator = new PhpMiddleware\RequestId\Generator\PhpUniqidGenerator();
$requestIdProvider = new PhpMiddleware\RequestId\RequestIdProviderFactory($generator);
$requestIdMiddleware = new PhpMiddleware\RequestId\RequestIdMiddleware($requestIdProvider);

$app = new MiddlewareRunner();
$app->add($requestIdMiddleware);
$app->run($request, $response);
```

All Provider factory constructor options:

- `PhpMiddleware\RequestId\Generator\GeneratorInterface` `$generator` - generator implementation (required)
- `bool|PhpMiddleware\RequestId\OverridePolicy\OverridePolicyInterface` `$allowOverride` (default `true`) - if `true` and request id header exists in incoming request, then value from request header will be used in middleware, using generator will be avoid
- `string` `$requestHeader` (default `X-Request-Id`) - request header name

How to get request id in my application?

- Middleware implements `RequestIdProviderInterface`, so you are able to use `getRequestId()` method,
- from `request-id` attribute in `ServerRequest` object (`$request->getAttribute(RequestIdMiddleware::ATTRIBUTE_NAME)`).

### Override policy

[](#override-policy)

You can add your own logic to decide when override incoming request id. You can implement `OverridePolicyInterface` and pass it as `$allowOverride` variable in constructor.

### Monolog processor

[](#monolog-processor)

We provide simple [Monolog](https://github.com/Seldaek/monolog) [processor](src/MonologProcessor.php) to add request it to every log entry!

### Request decorator

[](#request-decorator)

[RequestDecorator](src/RequestDecorator.php) adds header with request id to your request object. It's useful when your microservices communicate between using PSR-7 HTTP messages e.g. [Guzzle](https://github.com/guzzle/guzzle).

### Request Id generators

[](#request-id-generators)

To generate request id you need to use implementation of `PhpMiddleware\RequestId\Generator\GeneratorInterface`. There are predefined generators in `PhpMiddleware\RequestId\Generator\` namespace:

#### PhpUniqidGenerator

[](#phpuniqidgenerator)

Simple generator using [uniqid](http://php.net/manual/en/function.uniqid.php) function.

#### RamseyUuid1Generator

[](#ramseyuuid1generator)

[UUID](https://tools.ietf.org/html/rfc4122)1 implementations of [Ramsey\\Uuid](https://github.com/ramsey/uuid). To use it you need to add `ramsey/uuid` dependency to your `composer.json`.

#### RamseyUuid3Generator

[](#ramseyuuid3generator)

[UUID](https://tools.ietf.org/html/rfc4122)3 implementations of [Ramsey\\Uuid](https://github.com/ramsey/uuid). To use it you need to add `ramsey/uuid` dependency to your `composer.json`.

#### RamseyUuid4Generator

[](#ramseyuuid4generator)

[UUID](https://tools.ietf.org/html/rfc4122)4 implementations of [Ramsey\\Uuid](https://github.com/ramsey/uuid). To use it you need to add `ramsey/uuid` dependency to your `composer.json`.

#### RamseyUuid4StaticGenerator

[](#ramseyuuid4staticgenerator)

Generates Uuid4 like `RamseyUuid4Generator` however it's not require any dependency (it use static factory method).

#### RamseyUuid5Generator

[](#ramseyuuid5generator)

[UUID](https://tools.ietf.org/html/rfc4122)5 implementations of [Ramsey\\Uuid](https://github.com/ramsey/uuid). To use it you need to add `ramsey/uuid` dependency to your `composer.json`.

#### PrefixedGenerator

[](#prefixedgenerator)

It adds prefix to generated request id.

#### Md5Generator

[](#md5generator)

This generator converts generated request id to md5 hash.

It's just works with any modern php framework!
----------------------------------------------

[](#its-just-works-with-any-modern-php-framework)

Middleware tested on:

- [Expressive](https://github.com/zendframework/zend-expressive)

Middleware should works with:

- [Slim 3.x](https://github.com/slimphp/Slim)

And any other modern framework [supported middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html).

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance54

Moderate activity, may be stable

Popularity45

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 98.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~192 days

Recently: every ~326 days

Total

11

Last Release

2010d ago

Major Versions

1.0.0 → 2.0.02015-11-11

2.1.0 → 3.0.02016-01-22

3.1.2 → 4.02019-06-19

PHP version history (5 changes)1.0.0PHP &gt;=5.5

2.1.0PHP ^5.5 || ^7.0

3.1.0PHP ^5.6 || ^7.0

4.0PHP ^7.1

4.1.0PHP ^7.1 | ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/312655?v=4)[Witold Wasiczko](/maintainers/snapshotpl)[@snapshotpl](https://github.com/snapshotpl)

---

Top Contributors

[![snapshotpl](https://avatars.githubusercontent.com/u/312655?v=4)](https://github.com/snapshotpl "snapshotpl (77 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")

---

Tags

correlation-idmiddlewaremonologpsr-15psr-7request-iduuidpsrpsr-7middlewarerequest id

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/php-middleware-request-id/health.svg)

```
[![Health](https://phpackages.com/badges/php-middleware-request-id/health.svg)](https://phpackages.com/packages/php-middleware-request-id)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k19.1M1.7k](/packages/cakephp-cakephp)[mezzio/mezzio

PSR-15 Middleware Microframework

3893.8M118](/packages/mezzio-mezzio)[psr/http-server-middleware

Common interface for HTTP server-side middleware

18399.3M1.8k](/packages/psr-http-server-middleware)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28545.4k3](/packages/mezzio-mezzio-authentication-oauth2)[idealo/php-middleware-stack

Implementation of HTTP Middleware PSR-15 specification

3210.3k](/packages/idealo-php-middleware-stack)[eliashaeussler/typo3-solver

Solver - Extends TYPO3's exception handling with AI generated solutions. Problems can also be solved from command line. Several OpenAI parameters are configurable and prompts and solution providers can be customized as desired.

292.1k](/packages/eliashaeussler-typo3-solver)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
