Where should environment-specific values be saved in a Laravel 5 application? Where should environment-specific values be saved in a Laravel 5 application? docker docker

Where should environment-specific values be saved in a Laravel 5 application?


.env file will contain the variables which you will apply using env('variable') in the config files. The best practice would be set any parameters that are environment independent (meaning that will work on both production and develop) right in the config files and set any environment dependent variables in the .env file

Example: on your dev and staging server you will send email using gmail smtp but on production server you will use your own smtp server. Simply set the email configuration as follows:

config file:

return [    'driver' => 'smtp',    'host' => env('MAIL_HOST'),    'port' => env('MAIL_PORT'),    'from' => ['address' => env('MAIL_FROM_EMAIL'), 'name' => env('MAIL_FROM_NAME')],    'encryption' => 'tls',    'username' => env('MAIL_USERNAME'),    'password' => env('MAIL_PASSWORD'),    'sendmail' => '/usr/sbin/sendmail -bs',    'pretend' => false];

.env on dev and staging

MAIL_HOST=smtp.gmail.comMAIL_PORT=587MAIL_FROM_EMAIL=email@gmail.comMAIL_FROM_NAME=email@gmail.comMAIL_USERNAME=email@gmail.comMAIL_PASSWORD=password

.env on production

MAIL_HOST=smtp.domain.comMAIL_PORT=587MAIL_FROM_EMAIL=noreply@domain.comMAIL_FROM_NAME=noreply@domain.comMAIL_USERNAME=noreply@domain.comMAIL_PASSWORD=password

never include the .env file on your git repo. Instead make a copy and name it ".env.sample" and add that. So your dev team can setup their own .env file once the repo is cloned. You will need to add .env to the .gitignore to prevent it from adding to the repo.