How to set the global variable on Laravel Controller?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Set Global Variables in Laravel Controllers: Avoiding Repetition Through Best Practices

As developers working with frameworks like Laravel, one common hurdle we encounter is the repetitive nature of setup logic within controller methods. When you find yourself copying and pasting lines of code just to fetch the authenticated user, extract their name, or determine an ID across multiple functions, it signals that a better architectural pattern is needed.

The question you are asking—"How can I set global variables for common data in my controller?"—is fundamentally about managing scope and adhering to Separation of Concerns (SoC) within the MVC pattern. While PHP allows for true global variables, relying on them inside a request-scoped environment like a Laravel controller is generally considered poor practice, leading to brittle, hard-to-test code.

Instead of trying to create "global" variables, the professional approach is to encapsulate this shared data into structures that belong logically within your application architecture.

The Pitfall of Local Repetition

Let's look at the example you provided:

public function store(Request $request) 
{
   $user = auth()->user(); // Repeated logic
   $user_name = $user->name;
   $user_id = $user->id;
   // ... rest of the logic
}

public function signedPdf(Request $request) 
{
   $user = auth()->user(); // Repeated logic again
   $user_name = $user->name;
   $user_id = $user->id;
   // ... rest of the logic
}

This repetition violates the DRY (Don't Repeat Yourself) principle. If you ever need to change how user data is fetched (e.g., switching from the default auth()->user() to a custom service), you would have to update every single controller method, which increases the risk of bugs.

Solution 1: Encapsulation within the Controller Class

For simpler cases where related methods share context, you can use class properties to store data initialized once, perhaps in the constructor or an initial setup method. This keeps the variables scoped to that specific controller instance.

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    protected $currentUserData;

    public function __construct()
    {
        // Initialize common data once when the controller is instantiated
        $this->initializeUserData();
    }

    protected function initializeUserData()
    {
        $user = Auth::user();
        if (!$user) {
            // Handle case where user is not logged in
            throw new \Exception("User not authenticated.");
        }
        $this->currentUserData = [
            'id' => $user->id,
            'name' => $user->name,
            'email' => $user->email,
        ];
    }

    public function store(Request $request) 
    {
        // Access the shared data directly from the property
        $user_id = $this->currentUserData['id'];
        $user_name = $this->currentUserData['name'];

        // Now use $user_id and $user_name throughout the method
        $init_pdf = url('/') . '/images/upload/' . $user_name . '/' . $request->file('name');
        // ... rest of your logic
    }

    public function signedPdf(Request $request) 
    {
        $user_id = $this->currentUserData['id'];
        $user_name = $this->currentUserData['name'];
        
        $name = $user_name . $user_id . '.' . $request->file('extension');
        // ... rest of your logic
    }
}

This method is cleaner because the data initialization logic is separated from the business logic, making the controller methods themselves much easier to read and maintain.

Solution 2: The Recommended Approach – Service Classes

For larger applications or complex shared state that needs to be accessed across multiple controllers or services, the best practice in Laravel is to utilize Service Classes or Repositories. This adheres strictly to the Single Responsibility Principle and keeps your controllers focused solely on handling HTTP requests.

Instead of putting data fetching logic inside the controller, you delegate it to a dedicated service that handles the business logic.

// Example Service Class (app/Services/UserDataService.php)
class UserDataService
{
    public function getUserDetails()
    {
        $user = auth()->user();
        if (!$user) {
            throw new \Exception("User not found.");
        }
        return [
            'id' => $user->id,
            'name' => $user->name,
            'email' => $user->email,
        ];
    }
}

// In your Controller:
class UserController extends Controller
{
    protected $userService;

    public function __construct(UserDataService $userService)
    {
        $this->userService = $userService;
    }

    public function store(Request $request) 
    {
        // Delegate the complex fetching to the service
        $userData = $this->userService->getUserDetails();
        
        $user_id = $userData['id'];
        $user_name = $userData['name'];

        $init_pdf = url('/') . '/images/upload/' . $user_name . '/' . $request->file('name');
        // ... rest of your logic
    }
    // ... other methods
}

This Service-based approach is highly scalable. It makes your code more testable, promotes better architectural separation (a core tenet of good Laravel development), and aligns perfectly with the principles that drive robust software design, much like focusing on clean architecture within the Laravel ecosystem.

Conclusion

While you might think of "global variables" as a quick fix, in the context of modern PHP frameworks like Laravel, we seek solutions that improve structure and maintainability. For setting common data in controllers, I strongly recommend Solution 2: Using Service Classes. It ensures that your controller remains lean and focused on request handling, while complex data fetching and setup logic is neatly encapsulated elsewhere. Embrace dependency injection and service layers to build the robust, scalable applications that Laravel is designed to support.