php-middleware/request-id
最新稳定版本:4.1.0
Composer 安装命令:
composer require php-middleware/request-id
包简介
Request Id middleware with PSR-7
关键字:
README 文档
README
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
composer require php-middleware/request-id
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(defaulttrue) - iftrueand request id header exists in incoming request, then value from request header will be used in middleware, using generator will be avoidstring$requestHeader(defaultX-Request-Id) - request header name
How to get request id in my application?
- Middleware implements
RequestIdProviderInterface, so you are able to usegetRequestId()method, - from
request-idattribute inServerRequestobject ($request->getAttribute(RequestIdMiddleware::ATTRIBUTE_NAME)).
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
We provide simple Monolog processor to add request it to every log entry!
Request decorator
RequestDecorator 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.
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
Simple generator using uniqid function.
RamseyUuid1Generator
UUID1 implementations of Ramsey\Uuid. To use it you need to add ramsey/uuid dependency to your composer.json.
RamseyUuid3Generator
UUID3 implementations of Ramsey\Uuid. To use it you need to add ramsey/uuid dependency to your composer.json.
RamseyUuid4Generator
UUID4 implementations of Ramsey\Uuid. To use it you need to add ramsey/uuid dependency to your composer.json.
RamseyUuid4StaticGenerator
Generates Uuid4 like RamseyUuid4Generator however it's not require any dependency (it use static factory method).
RamseyUuid5Generator
UUID5 implementations of Ramsey\Uuid. To use it you need to add ramsey/uuid dependency to your composer.json.
PrefixedGenerator
It adds prefix to generated request id.
Md5Generator
This generator converts generated request id to md5 hash.
It's just works with any modern php framework!
Middleware tested on:
Middleware should works with:
And any other modern framework supported middlewares and PSR-7.
php-middleware/request-id 适用场景与选型建议
php-middleware/request-id 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 276.78k 次下载、GitHub Stars 达 36, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「psr」 「psr-7」 「request-id」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-middleware/request-id 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-middleware/request-id 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-middleware/request-id 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Simple PSR-16 cache implementations for WordPress transient the OOP way
Model of a web-based resource
Interfaces for web resource models and services to retrieve and create them
Interface for a factory to create Psr\Http\Message\StreamInterface objects
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 276.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 36
- 点击次数: 27
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知