Home > Backend Development > PHP Tutorial > Adding Request Context in Laravel Applications

Adding Request Context in Laravel Applications

Robert Michael Kim
Release: 2025-03-07 00:24:21
Original
806 people have browsed it

Adding Request Context in Laravel Applications

Laravel's Context facade enhances application insights by allowing you to add persistent metadata throughout the request lifecycle. This context automatically enriches your logs with valuable debugging information.

The following is a practical example of using request context in middleware and API request logging:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;

class ApiRequestLogger
{
    public function handle(Request $request, Closure $next)
    {
        // 添加基本的请求上下文
        Context::add('request_id', Str::uuid()->toString());
        Context::add('path', $request->path());
        Context::add('method', $request->method());
        // 如果已认证,则添加用户上下文
        if ($request->user()) {
            Context::add('user_id', $request->user()->id);
            Context::add('api_key', $request->user()->api_key);
        }
        // 添加性能指标
        $startTime = microtime(true);

        $response = $next($request);
        Context::add('response_time', round((microtime(true) - $startTime) * 1000, 2));
        Context::add('status_code', $response->getStatusCode());
        // 记录 API 请求
        Log::info('API request processed');

        return $response;
    }
}
Copy after login

Another example shows how to use Context Facade in a custom class:

<?php

use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class RequestContext
{
    public function __construct()
    {
        Context::add('request_id', Str::uuid()->toString());
    }

    public function addUserContext()
    {
        if (Auth::check()) {
            Context::add('user_id', Auth::id());
            Context::add('user_type', Auth::user()->type);
        }
    }

    public function logAction(string $action)
    {
        Log::info("User performed {$action}");
    }
}
Copy after login

Context facade enriches the logging of the application by providing valuable metadata that persists throughout the request lifecycle, making debugging and monitoring more efficient.

The above is the detailed content of Adding Request Context in Laravel Applications. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template