global variable in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Managing Shared State in Laravel: Ditching Global Variables for Robust Architecture In traditional PHP development, it was common practice to define variables within a file like `header.php` and expect them to be accessible across multiple pages—a concept often referred to as global state. While this approach works for small scripts, adopting it directly in a modern framework like Laravel introduces significant architectural risks. As senior developers building scalable applications, we must shift our mindset from procedural global variables to object-oriented, dependency-driven solutions. The core question is: How do we achieve the functionality of shared data across controllers without resorting to PHP's messy global scope? The answer lies not in creating true global variables, but in leveraging Laravel’s powerful service container and request lifecycle management. ## Why Avoid Global Variables in Laravel? Directly defining variables globally (e.g., using `$shared_number = 10;` outside of a class or function scope) violates the principles of encapsulation and testability that Laravel promotes. When data is scattered globally, it becomes extremely difficult to track where the data originates, leading to unpredictable bugs, especially in large, complex applications. The philosophy behind frameworks like Laravel is to manage state through explicit dependencies rather than implicit global access. If you need a number calculated by all controllers, that number should be treated as a dependency that is either injected or retrieved from a centralized source. ## The Laravel Way: Centralized State Management Since you require a variable (like a base number) that needs to be accessible across multiple controllers for calculations, the best practice involves moving this state into a location that the framework can manage and inject it where needed. Two primary methods are superior to defining true globals: ### 1. Configuration Files (For Static Application-Wide Data) If the variable holds static application settings—like a default tax rate, a base fee, or system constants—the ideal place is within your configuration files (`config/app.php` or custom config files). This data is loaded once and accessible via the `config()` helper, which is inherently safer and testable than global variables. **Example using Configuration:** If you need a base calculation number: ```php // config/app.php (or a custom config file) return [ 'calculation_base' => 100, // Our shared number ]; ``` In your controllers or services, you retrieve this value explicitly: ```php use Illuminate\Support\Facades\Config; class CalculationController extends Controller { public function performCalculation() { // Explicitly retrieving the shared state $base = Config::get('app.calculation_base'); $result = $base * 1.20; // Calculate using the shared value return response()->json(['result' => $result]); } } ``` This approach avoids global pollution while ensuring all parts of your application use the same, centrally managed source of truth. This aligns perfectly with the principles of robust architecture discussed in resources like those found on https://laravelcompany.com. ### 2. Service Classes and Dependency Injection (For Dynamic State) If the variable needs to be dynamic—meaning it changes based on the current request or complex business logic—it should be encapsulated within a dedicated Service Class. This class acts as a centralized manager for that specific piece of state. You then inject this service into any controller that requires it. **Example using a Service:** First, define the service: ```php // app/Services/CalculationService.php namespace App\Services; class CalculationService { protected int $baseValue; public function __construct(int $baseValue) { $this->baseValue = $baseValue; } public function calculateWithMarkup(float $markup): float { return $this->baseValue * (1 + $markup); } } ``` Then, bind and inject it into your controller: ```php use App\Services\CalculationService; class DynamicController extends Controller { // The framework handles injecting the dependency public function index(CalculationService $calculator) { $calculatedValue = $calculator->calculateWithMarkup(0.20); return response()->json(['value' => $calculatedValue]); } } ``` By using Dependency Injection, you ensure that the state is passed explicitly through the code flow, making your application highly modular, testable, and easy to maintain—a hallmark of high-quality Laravel development. ## Conclusion While the temptation to use simple global variables exists from procedural PHP backgrounds, in the Laravel ecosystem, we must embrace dependency management. For static data, lean on configuration files; for dynamic state that requires business logic, encapsulate it within dedicated Service Classes and utilize Dependency Injection. This architectural shift ensures that your application remains scalable, predictable, and adheres to modern object-oriented principles, making your code much more robust than relying on implicit global state.