7

How do I take advantage of the new .env files? I want to setup a local and production environment in Craft 3.

Anyone know how to do this or know of any docs out there explaining it ?

1 Answer 1

9

These are the two approaches I usually choose from:

Option 1: the 'default' Craft 3 way

  • Create a .env file for every environment you create, and add it to your .gitignore file (if it's not yet in it). In this file, you put all environment-specific details;
  • Craft, by default, will recognize and use those details.
  • In your Craft config files, you can use the ENVIRONMENT as defined in your .env file to adjust the settings for one specific environment. So, the config that applies all the time is set in an array with the key '*', and the config that's specifically for the dev environment, is in the array that has the 'dev' key (If you use the default general config file)

Option 2: the .env.php way

One of the reasons this approach exists, is that the makers of PHP dotenv (used in option 1) 'warn against using it in production' (as quoted from the README of option 2). The method as linked in the title uses a PHP file and some minor alterations to your config and index files to let you maintain a multi environment build.

1
  • 4
    The makers of phpdotenv (the lib Craft 3 uses to load .env files) warn against using it in production because of the overhead it adds to load the file on every request. It’s debatable if the “.env.php way” improves this situation in a measurable dimension. If you are really that much worried about performance in this order of magnitude (fractions of 1ms), you should better stick to getenv() and set the env variables on production servers via the server configuration files. Commented Apr 13, 2018 at 13:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.