4

What is/are the main reason(s), for which Symfony preferres to use .env files - for holding values of environment variables in them - instead of using the more flexible .yaml files?

I am asking this, because I read this comment, which states:

It was much simpler when we were using .yaml file.
I totally understand why we needed to switch to .env file, but IMHO we should rethink everything.

Thank you for your time.

PajuranCodes
  • 303
  • 3
  • 12
  • 43
  • One clear advantage to using env variables is that you can update their values without rebuilding the production cache. There are also a number of deployment strategies which use env variables. Having said that, I just use a parameters.yaml file. Still works just fine. – Cerad Apr 22 '19 at 19:41
  • Thank you @Cerad. Maybe you already answered my question, but I can't fully comprehend it. Sorry for that... My question is, actually, only related to the used file types. I mean, _why did Symfony preferred to recommend defining env vars in .env files, instead of in .yaml files_? Even if I don't use Symfony, I would love to define the env vars values of my own web MVC in .yaml files. Though I have some doubts, after seeing that Symfony and many other big projects utilize .env files. – PajuranCodes Apr 22 '19 at 20:05
  • 1
    Just to be clear, 99% of Symfony configuration is still done using yaml files. Only a few environment specific values or secret information is stored in .env files. And your question is just not a good fit for stackoverflow. sof wants to see specific questions. If you look in the community section in the Symfony documentation then you will see some Symfony specific discussion areas. And no one will tell you why you are being down voted since providing down vote details is considered to be "unkind". – Cerad Apr 22 '19 at 21:13
  • 1
    On `*NIX` operating systems, there's a shell command called `env`. Syntax used is what you see in `.env` files that are in use right now - `KEY=VALUE`. A `.env` doesn't have to exist. It's used to override existing values, on a per-environment basis. A `.env` is not used for configuration in Symfony or other popular frameworks - it's used to *supplement* it and my *guess* is that they're trying to follow the path paved long ago by people who worked on unix OS. A `.env` is simple, easy and multiple different projects (non-php) related are using it so it's really easy on devops to alter configs. – Mjh Apr 22 '19 at 21:31
  • Exactly in those few environment specific values and secret infos I am interested in, @Cerad. In short: would there be any negative effects, if I would store them in .yaml files, instead of in .env file, in my project? Thanks for the advices, btw! Now I finally know, why almost no one explained the downvotes on my answers/qusetions until now. – PajuranCodes Apr 22 '19 at 21:44
  • @Mjh Thank you! I found your comment very helpful. Personally, I think that it would be much easier on devops to alter their env configs in .yaml files, instead of .env files ;-) – PajuranCodes Apr 22 '19 at 21:59
  • 1
    Well, coming from a POV of a guy who does devops whole day (me), I can tell you that I and the rest of my coworkers prefer `.env` over other formats. Reason is really simple - it takes 0 time to learn the syntax, while with formats such as `.yaml` it's not the case. Sadly, we do have to work with `.yaml`, `.env`, `json` configuration, `.ini`, `.cnf`, `.conf` and what not which brings us to [XKCD: Standards](https://xkcd.com/927/) situation :) – Mjh Apr 23 '19 at 08:33
  • @Mjh Niiceee :-)) – PajuranCodes Apr 23 '19 at 10:01

1 Answers1

1

According to New in Symfony 3.2: Runtime Environment Variables:

Their main advantages are that they can be changed between deploys without changing any code and that they don't need to be checked into the code repository.

The same cannot be said about yaml files.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Nicodemuz
  • 3,539
  • 2
  • 26
  • 32
  • The blog author is trying to explain the env vars advantages with a phrase which, for us, some readers, can have multiple meanings. As a result, your sentence _"The same cannot be said about yaml files."_ is also abstract for me. Maybe you understood the blog entry better than me, Nicodemuz. Would you mind explaining me the presented principle(s) with clearer words? Especially the comparison that you accentuated. I think a little example will help, too... I am very interested in your updated answer, because I feel, that it will contain what I am looking for. Thank you in advance. – PajuranCodes Apr 23 '19 at 22:53
  • 2
    @dakis I think you should familiarise yourself with what environment variables are first. Maybe read the Wikipedia article on it: https://en.wikipedia.org/wiki/Environment_variable .. you should look at environment variables as variables that could potentially be accessed by multiple other applications also running on the same server. This way you can configure multiple apps directly by changing a single variable. Once you understand the benefits and purpose of environment variables, you will quickly realize that defining them in yaml files doesn't actually make them "environment variables". – Nicodemuz Apr 26 '19 at 06:50
  • Thanks. I see. My question might have been a bit confusing. Let me try to be clearer: On one hand, there are the _**"real" environment variables**_ (be they OS-wide, or the ones defined in virtual hosts with, for example, `SetEnv`). They are **automatically** saved by the PHP engine in the `$_SESSION` and `$_ENV` variables when a HTTP request is made (in browser, for example). On the other hand, there are the _**"custom" environment variables**_: those key/value pairs defined by developers in files with ".env" extension (in general). – PajuranCodes Apr 28 '19 at 19:02
  • On runtime, the mentioned files are **programmatically** read and the contained values override (or are added to) the "real" environment variables prior saved in the `$_SESSION` and `$_ENV` arrays... Well, I thought to start using .yaml files instead of .env files for defining "custom" env vars in my project. But I decided to research a bit on the web about it, first. Then I discovered, that Symfony had an important reason to still use .env files, even if they could use .yaml files instead. This reason I am trying to find out. – PajuranCodes Apr 28 '19 at 19:03
  • @dakis They could have used a .yaml file for those values. Probably they used an .env file because developers are already used to using that file for environment variables. – apaderno Mar 12 '23 at 18:26