Laravel 5环境配置数组? [英] Laravel 5 environment config arrays?

查看:231
本文介绍了Laravel 5环境配置数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 4中,您可以设置基于环境的配置文件夹结构:

In Laravel 4, you could set an environment based config folder structure:

/config/app.php
/config/dev/app.php
/config/staging/app.php
/config/testing/app.php

您可以使用Laravel 5做到这一点吗?我了解.env概念,并且正在使用它来定义我所处的环境.但是我需要定义一个配置值,该配置值是任意长度的数组,而对于.env文件,则无法做到这一点

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.

我要实现的目标的一个示例:

An example of what I'm trying to achieve:

if (in_array($request->input('value'), config('app.valid_values')) {
  // do something
}

valid_values只是一个值数组.它的长度是任意的,因此您不能像这样在.env文件中设置它们:

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_VALUE1=...
VALID_VALUE2=...
etc.

并且每个环境的阵列都需要不同.

AND the array needs to be different for each environment.

在Laravel 4中使用环境配置文件夹很容易做到这一点.但是,您如何使用Laravel 5做到这一点?

This was easy to do in Laravel 4 with environment configuration folders. But how do you do this with Laravel 5?

推荐答案

如果需要在值上创建数组,则可以创建字符串格式,并在需要时可以解析它们

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

何时需要它们

$myArrayValue  = explode(',', env('MY_ARRAY_VALUE'));

或者将您的值保存为JSON并通过json_decode()

Or save your values in JSON and get them with json_decode()

$myArrayValue  = json_decode(env('MY_ARRAY_VALUE'), true);

其他信息:

在Laravel 5上,您需要将所有配置文件转换为一个.env文件.

On Laravel 5, you need to translate all your configs files in one .env file.

在每个环境中,您的.env文件将与该环境的值不同.

On each environment your .env file will be diferent with values for this environment.

要设置环境,您需要在.env文件中更改APP_ENV的值

To set your environment, you need to change the value of APP_ENV in your .env file

APP_ENV=local

您可以在该文件中创建自己的变量

And you can create your own variables in that file

https://laravel.com/docs/5.2/configuration#environment-configuration

这是Laravel 5.0升级指南的摘录 https://laravel.com/docs/5.2/releases#laravel-5.0

This is an extract of the upgrade guide to Laravel 5.0 https://laravel.com/docs/5.2/releases#laravel-5.0

Laravel 5不再使用各种令人困惑的嵌套环境配置目录,而是使用了Vance Lucas的DotEnv.该库提供了一种超级简单的方法来管理环境配置,并使Laravel 5中的环境检测变得轻而易举.有关更多详细信息,请查阅完整的配置文档.

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.

您可以在此处找到默认的.env文件: https://github.com/laravel/laravel/blob/master/.env.example

You can find a default .env file here: 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.

为了使这一切顺利,Laravel利用了Vance Lucas的DotEnv PHP库.在全新的Laravel安装中,您的应用程序的根目录将包含.env.example文件.如果通过Composer安装Laravel,此文件将自动重命名为.env.否则,您应该手动重命名该文件.

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 5环境配置数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆