PHP code example of dragon-code / request-tracker

1. Go to this page and download the library: Download dragon-code/request-tracker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dragon-code / request-tracker example snippets


use DragonCode\RequestTracker\TrackerHeader;
use DragonCode\RequestTracker\TrackerRequest;
use Symfony\Component\HttpFoundation\Request;

/** @var Request $request */
$request = /* ... */;

$tracker = new TrackerRequest($request, new TrackerHeader);

function tracker(Request $request, ?int $userId = null): Request
{
    return (new TrackerRequest($request, new TrackerHeader))
        ->userId($userId)
        ->ip()
        ->traceId()
        ->getRequest();
}

// For the first call
tracker($request, $user->id);

// For subsequent services
tracker($request);

use DragonCode\RequestTracker\TrackerHeader;
use DragonCode\RequestTracker\TrackerRequest;
use Symfony\Component\HttpFoundation\Request;

/** @var Request $request */
$request = /* ... */;

$tracker = new TrackerRequest($request, new TrackerHeader);

function tracker(Request $request, ?int $userId = null): Request
{
    return (new TrackerRequest($request, new TrackerHeader))
        ->userId($userId)
        ->ip()
        ->traceId()
        ->custom('Some-Header', fn (Request $request) => 1234
        ->getRequest();
}

$item = tracker($request);

return $item->headers->get('Some-Header'); // 1234

$request->headers->set('Some-Header', 'qwerty');

$item = tracker($request);

return $item->headers->get('Some-Header'); // qwerty

use DragonCode\RequestTracker\TrackerHeader;

return new TrackerHeader(
    userId: 'Some-User-Id',
    ip: 'Some-IP',
    traceId: 'Some-Trace-Id',
);