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

Viewed 32466

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.

8 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

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

.env file contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env(‘KEY’).

The rule is that .env file is not committed to the repository, so it is really convenient, cause then people on your team can change their variables locally without committing them to the repository.

Now, .env.example file, on the contrary, is included in the repository – it is used as an example file for you to know what KEY=VALUE pairs you need for your project. Most often it is used to copy it to .env file and then change the values.

You can also read about it in the official Laravel documentation.

.env is simply used to store all sensitive files like password API key, database,and so on as environment variables to be used in your code later this sensitive files are not included in the code base and will not be there when been pushed to git. .env.example This is a file that tells other programmer what is meant to be in there code when your code is cloned or been used by another user.

Example .env API_KEY="hwhhwhshs6585gahwhgwuwjwusuhs"

.env.example API_KEY="YOUR API KEY GOES HERE"

The .env.example file is just an example of the .env file. It is not used by the app. It's used to serve as a base for you to edit and rename.

The .env file contains constants that are specific to that application to those environments. What this means is for example if I want to deploy my app in multiple places with the same code, I'll just have to change some settings on this file to run on each environment and we are all set, no code changes needed.

These settings can be database connection settings but they can be used for other things too like the APP_KEY that should be different for each application and used my many functions.

Be careful, this .env file should not be shared anywhere as it contains private information about that specific deploy.

You can read more here: https://laravel.com/docs/5.6/configuration#environment-configuration

From the version Laravel 5.0 in your main folder you should have .env file which contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env(‘KEY’).

The rule is that .env file is not committed to the repository, so it is really convenient, cause then people on your team can change their variables locally without committing them to the repository.

Now, .env.example file, on the contrary, is included in the repository – it is used as an example file for you to know what KEY=VALUE pairs you need for your project. Most often it is used to copy it to .env file and then change the values.

Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration. Furthermore, this would be a security risk in the event an intruder gains access to your source control repository, since any sensitive credentials would get exposed.

If you are developing with a team, you may wish to continue including a .env.example file with your application. By putting place-holder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application. You may also create a .env.testing file. This file will override the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.

.env or environment variables are files that store some sensitive information like your API key. They are only visible to you on your PC/local system. Not to anyone else, if you push your project to GitHub or some other platform.

.env is a file that store information about your website such as API or database password. these information are visible for you in local host. when you publish website on a host this file not visible for other people.actually env's file improve security website on the net.

Related