Back to Blog

Context Use for the Current Request Cycle with Global Data Sharing

Laravel 11 introduces an enhanced Context feature, revolutionizing how global data is handled within the current request cycle. Unlike view()->share(), which mainly serves Blade templates, Context provides a more flexible and application-wide approach to sharing data globally. Why Context is a Game Changer? The Context repository allows developers to store and retrieve request-specific data globally, […]

Context Use for the Current Request Cycle with Global Data Sharing

Laravel 11 introduces an enhanced Context feature, revolutionizing how global data is handled within the current request cycle. Unlike view()->share(), which mainly serves Blade templates, Context provides a more flexible and application-wide approach to sharing data globally.

Why Context is a Game Changer?

The Context repository allows developers to store and retrieve request-specific data globally, eliminating the need to manually pass values between controllers, middleware, and views. It enhances performance and improves code maintainability by centralizing data access.

view()->share() vs Context::add() and Context::get()

Traditionally, Laravel developers have used view()->share() to share data globally across Blade templates. However, this approach is limited to views and does not persist across the entire request cycle.

view()->share() Example:

view()->share('app_name', 'My Laravel App');
  • Accessible only within Blade views.
  • Not useful for middleware, controllers, or API responses.

Context::add() and Context::get() Example:

use Illuminate\Log\Context\Repository;

// Add data to the context
Context::add('app_name', 'My Laravel App');

// Retrieve data anywhere within the request cycle
$appName = Context::get('app_name');
  • Accessible globally, including middleware, controllers, services, and logs.
  • Works seamlessly within API responses and background jobs.

Comparison of Key Context Methods

add() vs addHidden()

  • add() stores data globally and makes it retrievable using get().
  • addHidden() stores sensitive or hidden data, retrievable only using getHidden().
Context::add('user_id', 123);
Context::addHidden('api_key', 'SECRET_KEY');

get() vs getHidden()

  • get() retrieves visible context data.
  • getHidden() retrieves hidden data that isn't exposed normally.
$userId = Context::get('user_id'); // 123
$apiKey = Context::getHidden('api_key'); // SECRET_KEY

all() vs allHidden()

  • all() retrieves all visible context data.
  • allHidden() retrieves only hidden data stored via addHidden().
$allData = Context::all();
$hiddenData = Context::allHidden();

How Context Integrates with Logging

Another powerful feature of Context is its ability to inject stored data into logs, making debugging easier.

Logging Context Data:

use Illuminate\Support\Facades\Log;

Context::add('request_id', request()->header('X-Request-ID'));
Context::add('user_id', auth()->id());

Log::info('User made a request', Context::all());

Sample Log Output:

[2024-03-19 12:00:00] local.INFO: User made a request {"request_id":"abcd-1234","user_id":5}

This makes troubleshooting and tracking user actions much more efficient.

Conclusion

Laravel 11’s Context is a game-changer for global data sharing across the request cycle. Unlike view()->share(), which is limited to Blade templates, Context allows global data access across middleware, controllers, services, and logs. With additional features like hidden context storage and enhanced logging integration, it significantly improves application debugging and performance.

If you're looking for custom Laravel web development, let's build something amazing together! 🚀