global variable for all controller and views

后端 未结 16 2146
Happy的楠姐
Happy的楠姐 2020-11-27 15:54

In Laravel I have a table settings and i have fetched complete data from the table in the BaseController, as following

public function __construct() 
{
             


        
16条回答
  •  青春惊慌失措
    2020-11-27 16:11

    I see, that this is still needed for 5.4+ and I just had the same problem, but none of the answers were clean enough, so I tried to accomplish the availability with ServiceProviders. Here is what i did:

    1. Created the Provider SettingsServiceProvider
        php artisan make:provider SettingsServiceProvider
    
    1. Created the Model i needed (GlobalSettings)
        php artisan make:model GlobalSettings
    
    1. Edited the generated register method in \App\Providers\SettingsServiceProvider. As you can see, I retrieve my settings using the eloquent model for it with Setting::all().
     
    
        public function register()
        {
            $this->app->singleton('App\GlobalSettings', function ($app) {
                return new GlobalSettings(Setting::all());
            });
        }
    
     
    1. Defined some useful parameters and methods (including the constructor with the needed Collection parameter) in GlobalSettings
     
    
        class GlobalSettings extends Model
        {
            protected $settings;
            protected $keyValuePair;
    
            public function __construct(Collection $settings)
            {
                $this->settings = $settings;
                foreach ($settings as $setting){
                    $this->keyValuePair[$setting->key] = $setting->value;
                }
            }
    
            public function has(string $key){ /* check key exists */ }
            public function contains(string $key){ /* check value exists */ }
            public function get(string $key){ /* get by key */ }
        }
    
     
    1. At last I registered the provider in config/app.php
     
    
        'providers' => [
            // [...]
    
            App\Providers\SettingsServiceProvider::class
        ]
    
     
    1. After clearing the config cache with php artisan config:cache you can use your singleton as follows.
     
    
        $foo = app(App\GlobalSettings::class);
        echo $foo->has("company") ? $foo->get("company") : "Stack Exchange Inc.";
    
     

    You can read more about service containers and service providers in Laravel Docs > Service Container and Laravel Docs > Service Providers.

    This is my first answer and I had not much time to write it down, so the formatting ist a bit spacey, but I hope you get everything.


    I forgot to include the boot method of SettingsServiceProvider, to make the settings variable global available in views, so here you go:

     
    
        public function boot(GlobalSettings $settinsInstance)
        {
            View::share('globalsettings', $settinsInstance);
        }
    
     

    Before the boot methods are called all providers have been registered, so we can just use our GlobalSettings instance as parameter, so it can be injected by Laravel.

    In blade template:

     
    
        {{ $globalsettings->get("company") }}
    
     

提交回复
热议问题