Installation
composer require chrisguitarguy/request-id-bundle
Add the bundle to config/bundles.php (Symfony 4+):
return [
// ...
Chrisguitarguy\RequestId\ChrisguitarguyRequestIdBundle::class => ['all' => true],
];
Verify Auto-Generation
The bundle automatically generates a Request-Id if no header is provided. Test with:
curl -v http://your-app.test
Check the response headers for Request-Id.
First Use Case: Logging Access the request ID in a controller or service:
use Chrisguitarguy\RequestId\RequestId;
public function someAction(RequestId $requestId)
{
$this->logger->info('Processing request', ['request_id' => $requestId->getId()]);
}
Request ID Injection Use dependency injection to access the request ID in any service:
public function __construct(private RequestId $requestId) {}
Middleware Integration Extend the bundle’s functionality by creating a custom middleware:
public function handle(Request $request, Closure $next)
{
$requestId = $request->headers->get('Request-Id');
// Custom logic (e.g., validate, transform)
return $next($request);
}
Logging and Monitoring Attach the request ID to logs (Monolog) or APM tools (e.g., New Relic):
$processor = new RequestIdProcessor($requestId);
$logger->pushHandler(new StreamHandler('app.log', LogLevel::DEBUG), $processor);
User-Facing Debugging Display the request ID in error pages or user notifications:
{% if app.requestId %}
<div class="debug-info">Request ID: {{ app.requestId }}</div>
{% endif %}
config/packages/chrisguitarguy_request_id.yaml.Request-Id header for traceability across microservices.RequestId service in PHPUnit:
$this->requestId = $this->createMock(RequestId::class);
$this->requestId->method('getId')->willReturn('TEST-123');
Header Trust Misconfiguration
trust_request_header: false, the bundle ignores incoming Request-Id headers.true in config if you want clients to provide IDs (e.g., for distributed tracing).Header Name Conflicts
request_header or response_header if they clash with existing headers (e.g., X-Request-ID).Thread Safety
Symfony 5+ Kernel Changes
config/bundles.php (not AppKernel).Missing Request ID? Check if the bundle is enabled and no middleware is stripping headers.
php bin/console debug:container chrisguitarguy_request_id.request_id
Header Not Propagated
Verify the response_header is correctly set in config and not overridden by other middleware.
Custom ID Generation Override the default UUID generator by binding a custom service:
# config/services.yaml
Chrisguitarguy\RequestId\RequestIdGenerator: '@app.custom_request_id_generator'
Event Listeners
Subscribe to kernel.request or kernel.response events to modify behavior:
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => ['onRequest', 10],
];
}
Database Storage Add the request ID to database logs or audit tables:
$entity->setRequestId($requestId->getId());
$entityManager->persist($entity);
Request-Id across services.trust_request_header is enabled (e.g., reject non-alphanumeric IDs).How can I help you explore Laravel packages today?