Skip to content
nive.sh

Loading index…

↑↓ navigate · ↵ opennive.sh

A better approach to keep Laravel .env files for multiple environments

Laravel comes with a .env file by default and it is called .env.example and we create a copy of .env.example file as .env...

Nivesh Saharan

· 2 min read · 250 words

Introduction

Laravel comes with a .env file by default and it is called .env.example and we create a copy of .env.example file as .env

.env.example should not be in .gitignore as it is to be used as an example environment file for you and your team. You must add all new environment variables that you add in .env file to .env.example file as well so your team can be in sync with what variables are required for this project to run.

Now let's say if you need to make copy of .env.example for several environments i.e. development, staging, production the approach I use to name those files are as below:

  • .env.development.env
  • .env.staging.env
  • .env.production.env

One of the benefit of doing this is we get syntax highlighting in IDEs like PhpStorm or editors like Visual Studio Code.

How to add .env files to .gitignore

We should have all our custom .env files in .gitignore file at the root of our project so we don't accidentally push them to Github or any other platform via git. Here's an example of a basic .gitignore file:

## Laravel's default .gitignore
 
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.phpunit.result.cache
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
 
## Custom added files
 
*.env # It will cover all your custom env files with .env extension

A gentle reminder

Do not use env() helper in your project anywhere outside of config files. Using config() instead of env() is super important when you enable caching!

Keep reading

Related articles

All articles

Jun 1, 2020 · 2 min read

Laravel/Php - How to serve gzipped content from S3

Until last month, our apps were serving JavaScript files from s3 without any compression and one of our customer asked us if we can enable gzip compression on the assets being served.

  • laravel
  • php
  • mysql

Jun 27, 2023 · 5 min read

Arrays and Tuples

There are two primitive compound types in Rust: Arrays and Tuples.

  • JavaScript
  • Rust

Newsletter

Build notes, when there is something worth sending.

New deep-dive articles and the occasional note on what I am shipping. No schedule, no filler, unsubscribe any time.

Taking on new projects

Need help with custom web and mobile engineering?

Tell me about the storefront, product or platform you’re working on. A 30-minute call is usually enough to map the first steps.

Email

hey@nive.sh

Local time

--:-- · Gurugram (IST)