DeveloperBreeze

Shared Data Development Tutorials, Guides & Insights

Unlock 1+ expert-curated shared data tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your shared data skills on DeveloperBreeze.

Using the Singleton Pattern to Optimize Shared Data in Laravel

Tutorial November 16, 2024
php

   namespace App\Http\Controllers;

   class HomeController extends Controller
   {
       protected $sharedData;

       public function __construct()
       {
           $this->sharedData = app('sharedData');
       }

       public function index()
       {
           return view('home', [
               'maxUploads' => $this->sharedData->maxUploads,
               'apiRateLimit' => $this->sharedData->apiRateLimit,
               'theme' => $this->sharedData->theme,
           ]);
       }
   }

You can also share the singleton data globally in views using the View::share method.