Laravel 5.5:如何定义可以在所有控制器中使用的全局变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51798773/
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-09-14 17:58:01  来源:igfitidea点击:

Laravel 5.5 : How to define global variable that can be used in all controllers ?

phplaravelglobal-variableslaravel-5.5controllers

提问by Saurabh Mistry

Hello Developers & Coders ,

你好开发人员和编码人员,

My question is How to define a global variable , that can be used in all controllers in Laravel ?

我的问题是如何定义一个全局变量,可以在 Laravel 的所有控制器中使用?

I have defined one variable $companyin AppServiceProviders's boot method- that im using in all blade views , but I can not use it in controllers file , it gives error , undefined variable $company

$companyAppServiceProviders 的启动方法中定义了一个变量- 我在所有刀片视图中使用它,但我不能在控制器文件中使用它,它给出了错误,undefined variable $company

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
        Schema::defaultStringLength(191);

        $company=DB::table('company')->where('id',1)->first();
        View::share('company',$company);  

    }

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

}

please guide me , thanks for your efforts & time :)

请指导我,感谢您的努力和时间:)

回答by kenken9999

set configuration variables at runtime

在运行时设置配置变量

class AppServiceProvider extends ServiceProvider
{
    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    public function boot()
    {
        View::share('key', 'value');
        Schema::defaultStringLength(191);

        $company=DB::table('company')->where('id',1)->first();
        // View::share('company',$company);  
        config(['yourconfig.company' => $company]);
    }
}

usage:

用法:

config('yourconfig.company');

回答by Kuba Bia?y

Okay, so unless you want to keep it in your Session, which I absolutely do not recommend, Cache which does not seem to be a best idea either or set it through the Config system inside of a Framework (which already are 3 different solutions suited for different matters) I would start from thinking what that variable will contain, if that's something that's just a Collection of Company model then you can basically use it in any controller by just using Laravel Eloquent methods.

好的,除非你想把它保留在你的会话中,我绝对不推荐,缓存这似乎也不是一个最好的主意,或者通过框架内的配置系统设置它(已经有 3 种不同的解决方案适合对于不同的问题)我会从考虑该变量将包含什么开始,如果这只是公司模型的集合,那么您基本上可以通过使用 Laravel Eloquent 方法在任何控制器中使用它。

What I recommend is either $company = Company::where('foo', 'bar')->first();, or just some data provider that would return all the information that you need in the form of Laravel Collection.

我推荐的是$company = Company::where('foo', 'bar')->first();,或者只是一些数据提供者,它会以 Laravel 集合的形式返回您需要的所有信息。

tl;dr

tl;博士

#recommended way that's reusable throughout whole app
Company::find(1); // instead of DB::table('company')->where('id', 1)->get();

Hope that helps.

希望有帮助。