Global Variable For All Controller and Views

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

global 

variable for all controller and views

ExampleFiles
global variable for all controller and views
In Laravel I have a table settings and i have fetched complete data from
the table in the BaseController, as following

public function __construct()  

    // Fetch the Site Settings object 
    $site_settings = Setting::all(); 
    View::share('site_settings', $site_settings); 

Now i want to access $site_settings. in all other controllers and views so
that i don't need to write the same code again and again, so anybody
please tell me the solution or any other way so i can fetch the data from
the table once and use it in all controllers and view.

Asked By: Deepak Goyal || Source

10 Amazing Walmart Deals This Week

Answer #1:
At first, a config file is appropriate for this kind of things but you may
also use another approach, which is as given below (Laravel ‐ 4):

// You can keep this in your filters.php file 
App::before(function($request) { 
    App::singleton('site_settings', function(){ 
global variable for all controller and views
        return Setting::all(); 
    }); 

    // If you use this line of code then it'll be available in any view 
    // as $site_settings but you may also use app('site_settings') as well
    View::share('site_settings', app('site_settings')); 
}); 

To get the same data in any controller you may use:

$site_settings = app('site_settings'); 

There are many ways, just use one or another, which one you prefer but I'm
using the Container.

Answered By: The Alpha

10 Amazing Walmart Deals This Week

Answer #2:
Okay, I'm going to completely ignore the ridiculous amount of over
engineering and assumptions that the other answers are rife with, and go
with the simple option.

If you're okay for there to be a single database call during each request,
then the method is simple, alarmingly so:

class BaseController extends Controller 

    protected $site_settings; 

    public function __construct()  
global variable for all controller and views
    public function __construct()  
    { 
        // Fetch the Site Settings object 
        $this‐>site_settings = Setting::all(); 
        View::share('site_settings', $this‐>site_settings); 
    } 

Now providing that all of your controllers extend this BaseController,
they can just do $this‐>site_settings.

If you wish to limit the amount of queries across multiple requests, you
could use a caching solution as previously provided, but based on your
question, the simple answer is a class property.

Answered By: ollieread

10 Amazing Walmart Deals This Week

Answer #3:
Use the Config class:

Config::set('site_settings', $site_settings); 

Config::get('site_settings'); 

http://laravel.com/docs/4.2/configuration

Configuration values that are set at run‐time are only set for the current
request, and will not be carried over to subsequent requests.

Answered By: malhal
global variable for all controller and views

10 Amazing Walmart Deals This Week

Answer #4:
In Laravel, 5+ you can create a file in the config folder and create
variables in that and use that across the app. For instance, I want to
store some information based on the site. I create a file called
site_vars.php, which looks like this

<?php 
return [ 
    'supportEmail' => '[email protected]', 
    'adminEmail' => '[email protected]
]; 

Now in the routes, controller, views you can access it using

Config::get('site_vars.supportEmail') 

In the views if I this

{{ Config::get('site_vars.supportEmail') }} 

It will give [email protected]

Hope this helps.

EDiT‐ You can also define vars in .env file and use them here. That is the
best way in my opinion as it gives you the flexibility to use values that
you want on your local machine.

So, you can do something this in the array
global variable for all controller and views
'supportEmail' => env('SUPPORT_EMAIL', '[email protected]') 

Important ‐ After you do this, don't forget to do

php artisan config:cache 

In case, there's still some problem, then you can do this (usually it
would never happen but still if it ever happens)

php artisan cache:clear 
php artisan config:cache 

Answered By: Koushik Das

Answer #5:
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 

2.  Created the Model i needed (GlobalSettings)

    php artisan make:model GlobalSettings 

3.  Edited the generated register method in
AppProvidersSettingsServiceProvider. As you can see, I retrieve my
settings using the eloquent model for it with Setting::all().

    public function register() 
    { 
        $this‐>app‐>singleton('AppGlobalSettings', function ($app) { 
            return new GlobalSettings(Setting::all()); 
        }); 
    } 

 
global variable for all controller and views

4.  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 */ } 
    } 

5.  At last I registered the provider in config/app.php

    'providers' => [ 
        // [...] 

        AppProvidersSettingsServiceProvider::class 
    ] 

6.  After clearing the config cache with php artisan config:cache you can
use your singleton as follows.

 
global variable for all controller and views

    $foo = app(AppGlobalSettings::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") }} 

Answered By: nbsp

Answer #6:
global variable for all controller and views

Most popular answers here with BaseController didn't worked for me on
Laravel 5.4, but they have worked on 5.3. No idea why.

I have found a way which works on Laravel 5.4 and gives variables even for
views which are skipping controllers. And, of course, you can get
variables from the database.

add in your app/Providers/AppServiceProvider.php

class AppServiceProvider extends ServiceProvider 

    public function boot() 
    { 
        // Using view composer to set following variables globally 
        view()‐>composer('*',function($view) { 
            $view‐>with('user', Auth::user()); 
            $view‐>with('social', Social::all());  
            // if you need to access in controller and views: 
            Config::set('something', $something);  
        }); 
    } 

credit:  http://laraveldaily.com/global‐variables‐in‐base‐controller/

Answered By: Gediminas

Answer #7:

View::share('site_settings', $site_settings); 

Add to

app‐>Providers‐>AppServiceProvider file boot method

it's global variable.

Answered By: Recai

Answer #8:
In Laravel 5+, to set a variable just once and access it 'globally', I
find it easiest to just add it as an attribute to the Request:
global variable for all controller and views
find it easiest to just add it as an attribute to the Request:

$request‐>attributes‐>add(['myVar' => $myVar]); 

Then you can access it from any of your controllers using:

$myVar = $request‐>get('myVar'); 

and from any of your blades using:

{{ Request::get('myVar') }} 

Answered By: spedley

Answer #9:
In Laravel 5.1 I needed a global variable populated with model data
accessible in all views.

I followed a similar approach to ollieread's answer and was able to use my
variable ($notifications) in any view.

My controller location: /app/Http/Controllers/Controller.php

<?php 

namespace AppHttpControllers; 

use IlluminateFoundationBusDispatchesJobs; 
use IlluminateRoutingController as BaseController; 
use IlluminateFoundationValidationValidatesRequests; 
use IlluminateFoundationAuthAccessAuthorizesRequests; 

use AppModelsMain as MainModel; 
use View; 

abstract class Controller extends BaseController 

    use AuthorizesRequests, DispatchesJobs, ValidatesRequests; 

    public function __construct() { 
        $oMainM = new MainModel; 
global variable for all controller and views
        $notifications = $oMainM‐>get_notifications(); 
        View::share('notifications', $notifications); 
    } 

My model location: /app/Models/Main.php

namespace AppModels; 

use IlluminateDatabaseEloquentModel; 
use DB; 

class Main extends Model 

    public function get_notifications() {... 

Answered By: Martin Carstens

Answer #10:
If you are worried about repeated database access, make sure that you have
some kind of caching built into your method so that database calls are
only made once per page request.

Something like (simplified example):

class Settings { 

    static protected $all; 

    static public function cachedAll() { 
        if (empty(self::$all)) { 
           self::$all = self::all(); 
        } 
        return self::$all; 
    } 

Then you would access Settings::cachedAll() instead of all() and this
would only make one database call per page request. Subsequent calls will
use the already‐retrieved contents cached in the class variable.

The above example is super simple, and uses an in‐memory cache so it only
global variable for all controller and views
lasts for the single request. If you wanted to, you could use Laravel's
caching (using Redis or Memcached) to persist your settings across
multiple requests. You can read more about the very simple caching options
here:

http://laravel.com/docs/cache

For example you could add a method to your Settings model that looks like:

static public function getSettings() { 
    $settings = Cache::remember('settings', 60, function() { 
        return Settings::all(); 
    }); 
    return $settings; 

This would only make a database call every 60 minutes otherwise it would
return the cached value whenever you call Settings::getSettings().

Answered By: Seb Barre

Answer #11:
I have found a better way which works on Laravel 5.5 and makes variables
accessible by views. And you can retrieve data from the database, do your
logic by importing your Model just as you would in your controller.

The "*" means you are referencing all views, if you research more you can
choose views to affect.

add in your app/Providers/AppServiceProvider.php

<?php 

    namespace AppProviders; 

    use IlluminateContractsViewView; 

    use IlluminateSupportServiceProvider; 
    use AppSetting; 

class AppServiceProvider extends ServiceProvider 

    /** 
     * Bootstrap any application services. 
     * 
global variable for all controller and views
     * 
     * @return void 
    */ 
    public function boot() 
    { 
        // Fetch the Site Settings object 
        view()‐>composer('*', function(View $view) { 
            $site_settings = Setting::all(); 
            $view‐>with('site_settings', $site_settings); 
        }); 
    } 

    /** 
     * Register any application services. 
     * 
     * @return void 
     */ 
    public function register() 
    { 

    } 

Answered By: Etimbuk

Answer #12:
You can also use Laravel helper which I'm using. Just create Helpers
folder under App folder then add the following code:

namespace AppHelpers; 
Use SettingModel; 
class SiteHelper 

    public static function settings() 
    { 
        if(null !== session('settings')){ 
          $settings = session('settings'); 
        }else{ 
          $settings = SettingModel::all(); 
          session(['settings' => $settings]); 
        } 
        return $settings; 
    } 
global variable for all controller and views

then add it on you config > app.php under alliases

'aliases' => [ 
.... 
'Site' => AppHelpersSiteHelper::class, 

1. To Use in Controller

use Site; 
class SettingsController extends Controller 

    public function index() 
    { 
        $settings = Site::settings(); 
        return $settings; 
    } 

2. To Use in View:

Site::settings() 

Answered By: Joesel T Duazo

Answer #13:
using middlwares

1‐ create middlware with any name

<?php 

namespace AppHttpMiddleware; 

use Closure; 
use IlluminateSupportFacadesView; 

class GlobalData 

global variable for all controller and views
    public function handle($request, Closure $next) 
    { 
        // edit this section and share what do you want 
        $site_settings = Setting::all(); 
        View::share('site_settings', $site_settings); 
        return $next($request); 
    } 

2‐ register your middleware in Kernal.php

protected $routeMiddleware = [ 
 . 
 ... 
 'globaldata' => GlobalData::class, 
 ] 

3‐now group your routes with globaldata middleware

Route::group(['middleware' => ['globaldata']], function () { 
//  add routes that need to site_settings 

Answered By: Mortada Jafar

Answer #14:
A global variable for using in controllers; you can set in
AppServiceProvider like this :

public function boot() 

    $company=DB::table('company')‐>where('id',1)‐>first(); 
    config(['yourconfig.company' => $company]); 

usage

config('yourconfig.company'); 

Answered By: Zahra Badri
global variable for all controller and views

Answer #15:
In file ‐ vendorautoload.php, define your gobals variable as follows,
should be in the topmost line.

$global_variable = "Some value";//the global variable 

Access that global variable anywhere as :‐

$GLOBALS['global_variable']; 

Enjoy :)

Answered By: Tom

Answer #16:
There are two options:

1.  Create a php class file inside app/libraries/YourClassFile.php

a. Any function you create in it would be easily accessible in all
the views and controllers.

b. If it is a static function you can easily access it by the class
name.

c. Make sure you inclued "app/libraries" in autoload classmap in
composer file.

2.  In app/config/app.php create a variable and you can reference the
same using

Config::get('variable_name');

Hope this helps.

Edit 1:

Example for my 1st point:

// app/libraries/DefaultFunctions.php 

class DefaultFunctions{ 
global variable for all controller and views
class DefaultFunctions{ 

    public static function getSomeValue(){ 
     // Fetch the Site Settings object 
     $site_settings = Setting::all(); 
     return $site_settings;  
    } 

//composer.json 

"autoload": { 
        "classmap": [ 
.. 
.. 
..   
        "app/libraries" // add the libraries to access globaly. 
        ] 
    } 

 //YourController.php 

   $default_functions  = new DefaultFunctions(); 
    $default_functions‐>getSomeValue(); 

Answered By: Chintan Parekh

The answers/resolutions are collected from stackoverflow, are licensed
under  cc by‐sa 2.5  ,  cc by‐sa 3.0  and  cc by‐sa 4.0  .

# More Articles
Why do Consumers accept lambdas with statement bodies but not expression bodies?

PHP, MySQL and Time Zones

Remove old Fragment from fragment manager

How to force node position (x and y) in graphviz

Cancel a timed event in Swift?

iOS 9 new feature Free Provisioning (Run your app on a device, just with your Apple ID,
without Apple developer membership)

Angular 2 cache observable http result data [duplicate]

What is the meaning of the “no index path for table cell being reused” message in iOS
global variable for all controller and views
What is the meaning of the “no index path for table cell being reused” message in iOS
6/7?

Add a Progress Bar in WebView

Custom UITableView section index

How to crop a CvMat in OpenCV?

What is the difference between pipe (|) and caret (^) attribute selectors?

Segmentation fault: 11 in OS X

Moving elements from std::vector to another one

Java: Split string when an uppercase letter is found

Use <c:forEach> with HashMap [duplicate]

How can I generate a cryptographically secure pseudorandom number in C#?

Bubbling scroll events from a ListView to its parent

crontab PATH and USER

You might also like