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?
short answer: yes, you can!
A simple way to update the . env key value in laravel is to add the below code in the controller function where you want to change . env values. $key = 'VARIABLE_NAME'; $value = 'NEW VALUE'; file_put_contents(app()->environmentFilePath(), str_replace($key .
env files to store credentials, one for each such environment (e.g. one . env file to contain development database keys and another to contain production database keys). To summarize, . env files contain credentials in key-value format for services used by the program they're building.
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.
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'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).
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With