The context features of Laravel provide a powerful method for tracking and managing data across various application components. This can greatly improve debugging, logging, and overall application management. Let's learn how you can use Laravel's context to improve your application using a real-world scenario.
The context feature in Laravel enables you to monitor and distribute information throughout various areas of your application, including requests, jobs, and commands. This information is seamlessly integrated into your logs, offering greater visibility into your application's performance and aiding in faster issue resolution.
Suppose you are developing an e-commerce application wherein you need to consider the order management system of a customer who has gone through the process of making an order right from the time the order was placed to the time when it has been delivered definition.
This includes various stages of an application like controllers, jobs, and notifications. Here’s how you can leverage Laravel's context to keep track of the order's journey.
When a customer places an order, you want to track the order details and may want to assign a unique trace ID to track the order throughout its lifecycle.
You can do this using middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;
class CaptureOrderContext
{
public function handle(Request $request, Closure $next)
{
if ($request->routeIs('placeOrder')) {
Context::add('order_id', $request->input('order_id'));
Context::add('trace_id', Str::uuid()->toString());
}
return $next($request);
}
}
This middleware captures the order_id
and generates a trace_id
for each order placement request.
When the order is processed by a job, you want to include the context information in the logs. Here’s how you can do that:
use Illuminate\Support\Facades\Log;
class ProcessOrderJob implements ShouldQueue
{
use Queueable;
public function handle()
{
Log::info('Processing order.', [
'order_id' => Context::get('order_id'),
'trace_id' => Context::get('trace_id'),
]);
}
}
The log entry will include both the order_id
and trace_id
, providing a clear link between the request and the job:
Processing order. {"order_id":12345} {"trace_id":"e04e1a11-e75c-4db3-b5b5-cfef4ef56697"}
If you want to track status updates sent via notifications or additional jobs, you can push information to a stack for historical tracking:
Context::push('status_updates', 'Order placed');
Later, you can retrieve and log the status updates:
Log::info('Order status updates:', ['updates' => Context::get('status_updates')]);
This could output:
Order status updates: {"updates":["Order placed","Order shipped","Order delivered"]}
The context features available through Laravel provide a convenient mechanism for tracking and managing data throughout your application. You can significantly improve traceability and the likelihood of debugging your application by gathering relevant data and sending it to jobs or including it in logs. This approach assists in maintaining consistency and reliability in the entire lifecycle of order processing as well as facilitating insight into how your program works.
Explore Laravel’s context capabilities in more detail in the official documentation
Sep 25, 2024
Understanding PHP Enums: A Simple GuideLearn how PHP Enums can make your code cleaner, more reliable, and easier to understand with type-safe, organised value management.
No Spam. Only high quality content and updates of our products.
Join 20,000+ other creators in our community