Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the need for env and env.example files in Laravel?

Tags:

php

laravel

I am new to Laravel and want a simple explanation of the .env and .env.example files, why we need them and the difference between them.

I know that .env is used to specify the app's database connection, for example but I would like to understand it deeper.

like image 844
Mirich Avatar asked Aug 29 '18 19:08

Mirich


2 Answers

.env file, as its name suggest, is a local where you put all your environment setup, such as database credentials, cache drivers and etc. Everything that is about the server that the project is running, and may have different values for different servers, are setup here.

Per example, your local dev environment has different database credentials than production environment. Also your colleague dev environment has different than yours. So each one has a .env with different informations.

And because of these, this file can't be versioned, so .env.example is the file that has every constants setups that .env has but with no values, and only this one is versioned. .env.example works as a guide for creating a .env file with the needed informations that is needs to have the application running.

As you are working with Laravel, you can find more informations here: environment-configuration

like image 104
Thiago B. Bittencourt Avatar answered Dec 08 '22 02:12

Thiago B. Bittencourt


The .env file stores configuration variables for your application and .env.example is simply an example of what might be in the .env file! You can easily rename .env.example to .env to get started.

What are configuration variables? From The Twelve-Factor App

An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes:

  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or Twitter
  • Per-deploy values such as the canonical hostname for the deploy

In Laravel the .env file also contains your app key which is used for encryption in your app. Because of this, and because you will likely store other private keys in this file, ensure you do not commit .env to your source control or share it publicly!

I recommend you read the link above for an explanation of why you should separate configuration from your application and for Laravel-specific information you can look here

like image 35
Josh Avatar answered Dec 08 '22 02:12

Josh