Laravel 5 环境配置数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37932031/
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
Laravel 5 environment config arrays?
提问by Jake Wilson
In Laravel 4, you could set an environment based config folder structure:
在 Laravel 4 中,您可以设置基于环境的配置文件夹结构:
/config/app.php
/config/dev/app.php
/config/staging/app.php
/config/testing/app.php
Can you do this with Laravel 5? I understand the .env
concept and I'm using that to define which environment I'm in. But I need to define a config value that is an array of arbitrary length, and you can't do that with .env
files.
你能用 Laravel 5 做到这一点吗?我理解这个.env
概念,我用它来定义我所在的环境。但我需要定义一个配置值,它是一个任意长度的数组,而你不能用.env
文件来做到这一点。
An example of what I'm trying to achieve:
我试图实现的一个例子:
if (in_array($request->input('value'), config('app.valid_values')) {
// do something
}
This valid_values
is simply an array of values. It's of arbitrary length, so you can't just set them in your .env
file like:
这valid_values
只是一个值数组。它的长度是任意的,所以你不能.env
像这样在你的文件中设置它们:
VALID_VALUE1=...
VALID_VALUE2=...
etc.
AND the array needs to be different for each environment.
并且每个环境的数组都需要不同。
This was easy to do in Laravel 4 with environment configuration folders. But how do you do this with Laravel 5?
这在带有环境配置文件夹的 Laravel 4 中很容易做到。但是你如何在 Laravel 5 中做到这一点呢?
回答by Sangar82
If you need to create an array on values, you can create on string format and when you need you can parse them
如果您需要在值上创建数组,您可以在字符串格式上创建,并且在需要时可以解析它们
MY_ARRAY_VALUE=1,2,house,cat,34234
When you need them
当你需要它们时
$myArrayValue = explode(',', env('MY_ARRAY_VALUE'));
Or save your values in JSON and get them with json_decode()
或者将您的值保存在 JSON 中并使用 json_decode()
$myArrayValue = json_decode(env('MY_ARRAY_VALUE'), true);
Extra info:
额外信息:
On Laravel 5, you need to translate all your configs files in one .env file.
在 Laravel 5 上,您需要将所有配置文件转换为一个 .env 文件。
On each environment your .env
file will be diferent with values for this environment.
在每个环境中,您的.env
文件将因该环境的值而异。
To set your environment, you need to change the value of APP_ENV
in your .env
file
要设置你的环境,你需要改变的价值APP_ENV
在你的.env
文件
APP_ENV=local
And you can create your own variables in that file
您可以在该文件中创建自己的变量
https://laravel.com/docs/5.2/configuration#environment-configuration
https://laravel.com/docs/5.2/configuration#environment-configuration
This is an extract of the upgrade guide to Laravel 5.0 https://laravel.com/docs/5.2/releases#laravel-5.0
这是 Laravel 5.0 升级指南的摘录 https://laravel.com/docs/5.2/releases#laravel-5.0
Instead of a variety of confusing, nested environment configuration directories, Laravel 5 now utilizes DotEnv by Vance Lucas. This library provides a super simple way to manage your environment configuration, and makes environment detection in Laravel 5 a breeze. For more details, check out the full configuration documentation.
Laravel 5 现在使用 Vance Lucas 的 DotEnv,而不是各种令人困惑的嵌套环境配置目录。这个库提供了一种超级简单的方法来管理您的环境配置,并使 Laravel 5 中的环境检测变得轻而易举。有关更多详细信息,请查看完整的配置文档。
You can find a default .env
file here: https://github.com/laravel/laravel/blob/master/.env.example
你可以.env
在这里找到一个默认文件:https: //github.com/laravel/laravel/blob/master/.env.example
It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different cache driver locally than you do on your production server. It's easy using environment based configuration.
To make this a cinch, Laravel utilizes the DotEnv PHP library by Vance Lucas. In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.
根据应用程序运行的环境使用不同的配置值通常很有帮助。例如,您可能希望在本地使用与在生产服务器上不同的缓存驱动程序。使用基于环境的配置很容易。
为了让这一切变得简单,Laravel 使用了 Vance Lucas 的 DotEnv PHP 库。在全新的 Laravel 安装中,应用程序的根目录将包含一个 .env.example 文件。如果你通过 Composer 安装 Laravel,这个文件会自动重命名为 .env。否则,您应该手动重命名文件。
回答by jessn
Generally for Phpdotenv
一般用于 Phpdotenv
Phpdotenv is about storing values in environment, not general purpose config library. Environment is UNIX concept and the values are always interpreted as character strings. Converting to different datatypes such as arrays or booleans even though convenient would be outside the scope of this class.
Phpdotenv 是关于在环境中存储值,而不是通用配置库。环境是 UNIX 概念,值总是被解释为字符串。转换为不同的数据类型,例如数组或布尔值,即使方便也超出了此类的范围。
Laravel config system
Laravel 配置系统
Laravel's config system is already separated. phpdotenv does environment, laravel does config. Then once config is done, environment is ignored. The concern of parsing environment variables from strings into whatever is passed on to laravel (weather that be their env function, or exploding inside your config files).
Laravel 的配置系统已经分离了。phpdotenv 负责环境,laravel 负责配置。然后一旦配置完成,环境将被忽略。将环境变量从字符串解析为传递给 laravel 的任何内容(天气是它们的 env 函数,或者在您的配置文件中爆炸)。
Good practice
良好做法
In other words, use Config::get() to get a specific conf file with your desired structure and you have what you need.
换句话说,使用 Config::get() 获取具有所需结构的特定 conf 文件,并且您拥有所需的内容。
You should never use env() in the code directly when it is outside of the config folder according to the Laravel guidelines. It's a good practice to use config(). In config files use env() to get the data from .env file.
根据 Laravel 指南,当 env() 位于 config 文件夹之外时,您不应该直接在代码中使用 env() 。使用 config() 是一个好习惯。在配置文件中使用 env() 从 .env 文件中获取数据。