在Laravel 5中设置全局变量 [英] Set Global variables in Laravel 5
本文介绍了在Laravel 5中设置全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将变量添加到可在Controller和View之间访问的Laravel框架的最佳方法是什么?
What is the best way to add variables to Laravel framework that is accessible across Controllers and Views?
我不想使用.env
来存储变量,因为它无法通过Git获得.
I don't want to use .env
for storing the variables as it wouldn't be available via Git.
推荐答案
您可以在config
文件夹中创建文件.例如
You can create a file within config
folder. e.g.
config
|- constants.php
在constants.php中,您可以定义全局变量
Inside constants.php you can define your global variables
<?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','contact@acme.inc'),
];
您可以使用以下两个函数之一来访问这些变量:
You can access these variable using either of these two functions:
Config::get('constants.company_name')
config('constants.company_name')
对于Blade也:
{{ config('constants.company_email') }}
这篇关于在Laravel 5中设置全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文