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