php Laravel 中的全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19808715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:04:56  来源:igfitidea点击:

global variable in laravel

phplaravellaravel-4global-variables

提问by Pars

In PHP, I used to define some variables in my header.phpand use them in all my pages. How can I have something like that in Laravel?

在 PHP 中,我曾经在 my 中定义一些变量并在我的header.php所有页面中使用它们。我怎么能在 Laravel 中拥有这样的东西?

I am not talking about View::share('xx', 'xx' );

我不是在谈论 View::share('xx', 'xx' );

Assume I want to have a variable which holds a number in it and I need this number inside all my controllers to calculate something.

假设我想要一个变量,其中包含一个数字,并且我需要在我的所有控制器中使用这个数字来计算一些东西。

回答by fideloper

Sounds like a good candidate for a configuration file.

听起来像是配置文件的一个很好的候选者。

Create a new one, let's call it calculations.php:

创建一个新的,我们称之为calculations.php

Laravel ~4ish:

Laravel ~4ish:

app
    config
        calculations.php

Laravel 5,6,7+:

Laravel 5,6,7+:

config
    calculations.php

Then put stuff in the new config file:

然后把东西放在新的配置文件中:

<?php return [ 'some_key' => 42 ];

Then retrieve the config in your code somewhere (note the file name becomes a "namespace" of sorts for the config item):

然后在您的代码中的某处检索配置(请注意,文件名成为配置项的某种“命名空间”):

echo Config::get('calculations.some_key'); // 42 in Laravel ~4
echo config('calculations.some_key'); // 42 in Laravel ~5,6,7+

回答by Jacob

Set a property on the BaseController, which should be located in your controllersdirectory.

在 上设置一个属性BaseController,它应该位于您的controllers目录中。

Your controllers should extend the BaseControllerclass and thus inherit its properties.

您的控制器应该扩展BaseController该类,从而继承其属性。

回答by Garret Blankenship

You could use View Composers

您可以使用View Composers

And instead of using the boot method described in the docs you could use:

而不是使用文档中描述的引导方法,您可以使用:

public function boot()
{
    // Using class based composers...
    view()->composer(
        '*', 'App\Http\ViewComposers\ProfileComposer'
    );

    // Using Closure based composers...
    view()->composer('*', function ($view) {

    });
}

That would render whatever variables you declare with

这将呈现您声明的任何变量

$view->with('yourVariableName', 'yourVariableValue');

to all the views in your app.

应用程序中的所有视图。

Here is a full example of how I used this in one of my projects.

这是我如何在我的一个项目中使用它的完整示例。

app/Providers/ComposerServiceProvider.php

app/Providers/ComposerServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
     /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer(
            '*', 'App\Http\ViewComposers\UserComposer'
        );
    }

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

app/Http/ViewComposers/UserComposer.php

应用程序/Http/ViewComposers/UserComposer.php

<?php

namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;
use Illuminate\Contracts\Auth\Guard;

class UserComposer
{

    protected $auth;

    public function __construct(Guard $auth)
    {
        // Dependencies automatically resolved by service container...
        $this->auth = $auth;
    }

    public function compose(View $view)
    {
        $view->with('loggedInUser', $this->auth->user());
    }
}

Just remember that because you declared a new service provider it needs to be included in the 'providers' array in config/app.php

请记住,因为您声明了一个新的服务提供者,所以它需要包含在config/app.php的 'providers' 数组中