0
BimaAdi
3y

I see some of web framework use .env to store configuration file (eg Laravel, CI). Is there any benefits to store configuration file on .env file? Is it consider a good practice?

Comments
  • 1
    The purpose of it is to have all the environmental variables on one place. So if you move from localhost to distribution, you only have to change one file, it contains all the database names, passwords, domain name, so everything about the environment. It is really beneficial and good practice, makes your life much easier. :)
  • 1
    .env means you can have a DEV vs PROD environment details and not use PROD resources or assets or files or a MySQL database when your building things.

    The code base is exactly the same, so you remove the prod checks (for example)

    if($env == "prod")

    to do the same and rely on the .env file already containing it.

    Another way was to add a .env and have the php.ini serve it on every request.
  • 1
    This is probably a better explanation

    https://github.com/vlucas/phpdotenv
  • 1
    It's framework specific. But .env is by far the most commonly used convention.

    It's a good question and I remember having these when I had just started coding professionally.

    Quick summary -

    .env will be machine (physical/container) specific

    You don't commit your .env file to source control (there are situations where you would, but there always are exceptions)

    Example : .env on your machine has a db config for local db and .env on production has a db config for production, the app runtime just picks up the same file everytime.

    This is the core idea. There are setups where you have config files like prod.conf, local.conf and the .env file toggles which one to pick (so you could connect to multiple dbs - prod from local - as from the above example)
  • 1
    I do mine in either yaml, json or whatever language I am using like .rb or .py

    I rather not deal with env variables but that id a preference
Add Comment