namespace App\Controller;
use MdavidDev\SymfonyCorrelationIdBundle\Service\CorrelationIdStorage;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class UserController extends AbstractController
{
public function __construct(
private readonly CorrelationIdStorage $correlationIdStorage
) {
}
#[Route('/api/users', methods: ['GET'])]
public function list(): JsonResponse
{
$correlationId = $this->correlationIdStorage->get();
$this->logger->info('Fetching users', [
'correlation_id' => $correlationId,
]);
return $this->json(['users' => []]);
}
}
// Get current correlation ID
$id = $this->correlationIdStorage->get();
// Check if ID exists
if ($this->correlationIdStorage->has()) {
// ...
}
// Set custom ID (advanced usage)
$this->correlationIdStorage->set('my-custom-id-123');
// Clear the ID
$this->correlationIdStorage->clear();
use Symfony\Component\Process\Process;
class MyService
{
public function __construct(
private readonly CorrelationIdStorage $correlationIdStorage
) {}
public function executeCommand(): void
{
// Get current correlation ID from HTTP request
$correlationId = $this->correlationIdStorage->get();
// Pass it to the console command via environment variable
$process = new Process(
['php', 'bin/console', 'app:my-command'],
env: ['CORRELATION_ID' => $correlationId]
);
$process->run();
}
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$correlationId = $this->correlationIdStorage->get();
// ...
}
namespace App\Service;
use MdavidDev\SymfonyCorrelationIdBundle\Service\Generator\CorrelationIdGeneratorInterface;
class CustomIdGenerator implements CorrelationIdGeneratorInterface
{
public function generate(): string
{
return uniqid('APP-', true);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.