简体   繁体   English

如何在Laravel中将变量共享给所有控制器和视图?

[英]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. 我有一个Laravel 5.5项目,我需要在所有控制器和所有视图中共享一些变量,并具有一些全局默认值,然后可以在各种控制器方法中编辑这些默认值,最后这些视图将显示那些更新的变量。

For example: 例如:

$test = [1, 2, 3];

in fooController.php BarMethod I would say: fooController.php BarMethod中,我会说:

$test[] = 4;

Then the view file that is accompanied with that BarMethod would see [1, 2, 3, 4] . 然后,带有该BarMethod的视图文件将看到[1, 2, 3, 4]

But if I said something else in testController.php BazMethod: 但是,如果我在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] . 然后属于该方法的另一个视图将看到[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 . 好的,我们要澄清一下:我正在谈论多个请求,第一个请求将发送到fooController ,第二个请求将发送到testController ,第n个请求将发送到某些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. 在所有情况下,都有初始的$test变量及其默认值,然后控制器中所有请求的方法都可以更改默认的初始变量,然后将其发送到视图。

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. 例如,我可以通过在baseController设置$test的默认值,然后更新这两个方法中的值来实现所有这些,但是我的真正问题是我需要在要发送到的变量列表中指定变量所有方法中的视图。

So what I'd need is a way automatically send that updated $test variable to all views. 因此,我需要一种自动将更新的$test变量发送到所有视图的方法。 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文件。

config/global.php , Then add the below code config/global.php ,然后添加以下代码

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. 您可以创建一个中间件来填充Configuration变量,所有框架中的配置始终可用。

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. 如果您不喜欢为此目的使用config,则可以创建一个Service Class(不是Service Provider),并将其声明为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. 如您所见,其行为类似于config,但是通过这种方法,您可以防止错误地即时覆盖某些配置。 The code can be improved with checks and other methods, this was a quick example of how you can do it. 可以使用检查和其他方法来改进代码,这是如何执行此代码的快速示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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