Ignition and Flare can now display Laravel 11's context
Laravel 11 recently introduced a nice new feature called Context. Both Ignition and Flare have been updated to collect and show all context.
What is Context?
Laravel's latest feature allows you to set some contextual values anywhere in your app. This allows you to store and exchange information across different requests, jobs, and commands in your application. This information is written alongside logs, aiding in understanding the sequence of events leading up to the log entry or error.
use Illuminate\Support\Facades\Context;
Context::add('url', $request->url());
Context::add('trace_id', Str::uuid()->toString());
You can easily retrieve these values anywhere in your app.
use Illuminate\Support\Facades\Context;
Context::all(); // returns all context
Now, this isn’t all that special. With the examples given above, you’d be right to think that Context is just some array-backed cache.
But the special thing that Context does is make all of this information available to any job dispatched in the request where context was set.
dispatch(new MyJob()); // this job will receive the context
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Context;
class MyJob implements ShouldQueue
{
public function handle()
{
// returns the context that was
// set in the request that dispatched
// this job
$allContent = Context::all();
// do some work
// ...
}
}
Context in Ignition and Flare
Ignition, the error page we created and used in Laravel by default, now displays all set context.
Here’s what that looks like in Ignition.
Of course, Flare now collects and displays context as well. Here’s what that looks like when you have an exception in a request or job that holds context.