Laravel 5 environment config arrays?

Generally for 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.

Laravel config system

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

Good practice

In other words, use Config::get() to get a specific conf file with your desired structure and you have what you need.

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.


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.

Tags:

Php

Laravel