在 Laravel 5 中设置全局变量

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

Set Global variables in Laravel 5

phplaravel

提问by Santosh Achari

What is the best way to add variables to Laravel framework that is accessible across Controllers and Views?

将变量添加到可跨控制器和视图访问的 Laravel 框架的最佳方法是什么?

I don't want to use .envfor storing the variables as it wouldn't be available via Git.

我不想.env用于存储变量,因为它无法通过 Git 获得。

回答by Santosh Achari

You can create a file within configfolder. e.g.

您可以在config文件夹中创建一个文件。例如

config
|- constants.php

Inside constants.php you can define your global variables

在 constants.php 中,您可以定义全局变量

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | User Defined Variables
    |--------------------------------------------------------------------------
    |
    | This is a set of variables that are made specific to this application
    | that are better placed here rather than in .env file.
    | Use config('your_key') to get the values.
    |
    */

    'company_name' => env('COMPANY_NAME','Acme Inc'),
    'company_email' => env('COMPANY_email','[email protected]'),


];

You can access these variable using either of these two functions:

您可以使用以下两个函数之一访问这些变量:

Config::get('constants.company_name')
config('constants.company_name')

For Blade as well:

对于 Blade 也是:

{{ config('constants.company_email') }}