在 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
Set Global variables in Laravel 5
提问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 .env
for storing the variables as it wouldn't be available via Git.
我不想.env
用于存储变量,因为它无法通过 Git 获得。
回答by Santosh Achari
You can create a file within config
folder. 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') }}