Download the PHP package semaio/request-id-bundle without Composer
On this page you can find all versions of the php package semaio/request-id-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download semaio/request-id-bundle
More information about semaio/request-id-bundle
Files in semaio/request-id-bundle
Package request-id-bundle
Short Description This bundle adds request IDs to the request and response in your Symfony application.
License MIT
Informations about the package request-id-bundle
Symfony Request ID Bundle
This bundle adds request IDs to the request and response in your Symfony application.
Why? It is a great and simple way to add some additional information to logs and to present to users. For example, if a request fails or an exception is thrown you'll be able to show the user the request ID which they can pass on to you to locate their specific issue.
Installation
The request ID bundle can be installed at any point during a project's lifecycle.
Requirements
- PHP 8.2 / PHP 8.3
- Symfony 7.0
Install the bundle
Please install the bundle using composer:
Enable the bundle
Then, enable the bundle by adding the following line in bundles.php
file of your project:
Configure the bundle
Now, that the bundle is installed and enabled, you have to add some configuration:
How it works
When a request comes in your Symfony application and if your configured policy allows it, the bundle inspects
the X-Request-Id
header. If present, the given value will be used throught the rest of your request lifecycle in your
Symfony application. This lets you use request ID's from somewhere higher up in the stack (like in the web server itself
or a load balancer).
If no request ID is found or configured policy rejects using any given X-Request-Id
header from the incoming request,
the bundle creates a new request ID based on the configured generator. By default, a UUID version 4 request ID is
generated (example: 31c70a8e-8a1e-47af-9c31-3285e9bc2eb3
).
Before sending the response to the client, the X-Request-Id
header is also set on the response using the generated
request ID value.
Components
Generators
Generators create a random string that will be used as request ID throughout the request lifecycle of your Symfony application.
All generators must implement the Semaio\RequestId\Generator\GeneratorInterface
. By default, there are three possible
generators:
RamseyUuid4Generator
- This generator creates a UUID v4 request ID by leveraging the ramsey/uuid library.
- This is the default generator!
PhpUniqidGenerator
- This generator creates a request ID with PHP's native uniqid function.
Md5Generator
- This generator creates a request ID through an injected generator (default:
RamseyUuid4Generator
) and then hashes the request ID with PHP's native md5 function.
- This generator creates a request ID through an injected generator (default:
Policies
Policies check the incoming request for two reasons:
- Should a request ID be added to the current request?
- If the current request already contains a request ID, should this value be trusted or should the bundle create a new request ID?
All policies must implement the Semaio\RequestId\Policy\PolicyInterface
. By default, there are two possible policies:
DefaultPolicy
- Allows request ID to be added to the request and also accepts any given incoming request ID.
- This is the default policy!
RejectRequestIdHeaderPolicy
- Allows request ID to be added to the request but rejects any given incoming request ID.
Providers
Providers hold the generated request ID and provide it to any part of your code where you might need the request ID. By default, there is only one provider:
SimpleRequestIdProvider
- Just a simple getter-setter PHP object.
- This is the default provider!
Extensions
Monolog integration
This bundle provides a monolog processor which adds the request ID to extra
array on the record. This can be turned
off by setting enable_monolog
to false
in bundle configuration.
To use the request ID in your logs, include %extra.request_id%
in your formatter. Here's a configuration example from
this bundle's tests.
Twig integration
This bundle provides a global request_id
function in your Twig environment. This can be turned off by
setting enable_twig
to false
in bundle configuration.
Here's an example of a template.
License
MIT
All versions of request-id-bundle with dependencies
monolog/monolog Version ^3.0
ramsey/uuid Version ^4.7
symfony/framework-bundle Version ^7.0