Context
Inject a value from the application context
Description: Injects a value from the application's shared context.
Namespace: Illuminate\Container\Attributes\Context
Usage
use Illuminate\Container\Attributes\Context;
class RequestLogger
{
public function __construct(
#[Context('trace_id')] private readonly ?string $traceId
) {}
public function log(string $message): void
{
logger()->info($message, ['trace_id' => $this->traceId]);
}
}
Also supports hidden context:
#[Context('secret_key', hidden: true)] private readonly ?string $secret