简体   繁体   中英

How to share variables to all controllers and views in Laravel?

I have a Laravel 5.5 project where I would need to share some variables throughout all controllers and all views with the option to have some global defaults and then those defaults could be edited within the various controller methods, and in the end the views would show those updated variables.

For example:

$test = [1, 2, 3];

in fooController.php BarMethod I would say:

$test[] = 4;

Then the view file that is accompanied with that BarMethod would see [1, 2, 3, 4] .

But if I said something else in testController.php BazMethod:

$test = array_merge($test, [100, 1000, 10000]);

Then my other view that belongs to this method would see [1, 2, 3, 100, 1000, 10000] .

Any ideas are welcome.

Thanks.


Okay, so to clarify: I am talking about multiple requests, the 1st request would go to fooController , the 2nd one to testController , the nth one to some nthController . In all cases there would be the initial $test variable with its default value, then all the requested methods in the controllers could alter the default initial variable and then send it to the view.

All this I can achieve for example by having the default values of $test set in a baseController , then update the values in either of the methods, but my real issue is that I need to specify the variable among the list of variables to send to the view in all methods.

So what I'd need is a way automatically send that updated $test variable to all views. I was looking at service providers, but it seemed to me that a service provider would do its job before editing the values, so the initial value would show up in the view, not the updated value.

I honestly don't think sessions could help in this situation.

First we have to create global.php file in config folder.

config/global.php , Then add the below code

return [
    'clientid' => '2',
    'code' => 'xyz
];

Access the global varibale like below

config('global.clientid')

You can create a Middleware to populate a Configuration variable, configuration is always available from all the framework.

public function handle()
{
    config(['myCustomConfig.email' => 'me@example.com']);
}

// index.blade.php

<div>{{ config('myCustomConfig.email') }} </div>

Saving the value on the config make it available from views, controllers or even models.

If you don't like to use config for that purpose, you can create a Service Class (not Service Provider), and declare it as a Singleton.

// app/Services/SharingService.php

namespace App\Services;

class SharingService {

    private $shared = [];

    public function share($name, $value)
    {
        $this->shared[$name] = $value;
    }

    public function get($name, $default = null)
    {
        if (isset($this->shared[$name])) {
            return $this->shared[$name];
        }

        return $default;
    }
}

// AppServiceProvider.php

$this->app()->singleton('shared', function() {

    $sharingService = new SharingService();

    $sharingService->share('email', 'me@example.com');
    // ☝️ you can set values here or in any place, since it's a public method

    return $sharingService;
});

// index.blade.php

<div>{{ app('shared')->get('email') }}</div> <!-- me@example.com -->

// any Controller or model

$email = app('shared')->get('email');

As you can see, the behaviour is like config, but with this approach you're preventing overriding some configurations on the fly by mistake. The code can be improved with checks and other methods, this was a quick example of how you can do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM