Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

php - Laravel 5 environment config arrays?

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

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
}

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

$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.

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

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

This is an extract of the upgrade guide to 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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...