Search code examples
laravellaravel-5laravel-5.3

Laravel super/root controller?


Is in laravel possible to create a super controller that can share his result with all other controllers? Like id's... I needed it to share a "board id", because inside of that I have some actions where this information is globaly needed


Solution

  • You can share data with each controllers, by just extending a BaseController like this:

    BaseController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use App\Http\Controllers\Controller;
    
    class BaseController extends Controller
    {
        public $yourVariable = 'some_data';
    }
    

    UserController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use App\User;
    use App\Http\Controllers\BaseController;
    
    class UserController extends BaseController
    {
        public function show($id)
        {
            // Do your stuff here (use BaseController's common variables here)
            $var = $this->yourVariable;
        }
    }
    

    Hope this helps!