-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .env file support #13602
Comments
can you elaborate on this further please? Can you include a link & explanation about the |
@dynasource Of course. Just a quick example. We want to move from
Than I want to change DB configuration, etc. I think, that all environmental things could be stored in Example of Laravel
It could be much easier enter one file, set up your |
Wouldn't this make more sense in an app template, i.e. in a config file like https://github.com/yiisoft/yii2-app-basic/blob/master/config/web.php? There you can read configuration from environment variables and set them in the configuration array. I don't think that this should be part of the yii2 core. |
I have done something like that. I did a simple change in //web/index.php
require(__DIR__ . '/../vendor/autoload.php');
(new Dotenv\Dotenv(__DIR__ . '/../'))->load();
defined('YII_DEBUG') or define('YII_DEBUG', getenv('APP_DEBUG'));
defined('YII_ENV') or define('YII_ENV', getenv('APP_ENV')); This allows to remove I am not using more the |
It's not only for fast switching. The main problem, is that app templates could become quite big. Maybe this issue should be created at the advanced and base app templates repo, cause I can add this each time, but it would be great to have such feature out of the box. I saw this feature in Laravel and one custom composer-oriented WP boilerplate. And I even thought, that it's was my fault, that I haven't used it in other projects and it's an essential feature of PHP itself, cause it looks very simple and elegant for me :) |
@Denis4yk, thanks for explaining. Having a |
Please keep in mind, that people already use |
For some more ideas, here's our helper class to load configuration from environment vars in our dockerized yii2 apps: https://github.com/codemix/yii2-dockerized/blob/master/helpers/DockerEnv.php. The core methods relevant for this issue being We load it from our <?php
require('/var/www/html/helpers/DockerEnv.php');
\DockerEnv::init();
$config = \DockerEnv::webConfig();
(new yii\web\Application($config))->run(); and configure the app in 'log' => [
'traceLevel' => \DockerEnv::get('YII_TRACELEVEL', 0),
... If we had something similar in yii2's core, it would still not be mandatory. Developers can still modify |
agree. Moved to yiisoft/yii2-app-basic#108 |
Related: We've just released your config loader https://github.com/codemix/yii2-configloader. It can do much more, but can also be used to just read environment variables: // Directory where .env can be found
Config::initEnv('/path/to/app');
$setting = Config::env('MY_VAR', 'default'); |
Looks handy. Could be announced at http://yiifeed.com/ |
Probably the idea of having a file to keep variables that only refers to the development system is not bad, but what really sucks is to have them in an .env file. That is something that I hate from Laravel, because al the end, you use it for any type of configuration parameter (Laravel puts there the app name, the database credentials and much more). Moreover, the .env files are very limited and difficult to set up (newlines, quotes, etc.). If you want to add this to Yii, please, use a .php file instead. |
its not going to be part of the framework, if there will be anything official, it would be part of an application template. |
https://github.com/lav45/yii2-project-configuration
|
I started using .env files in one of my projects, I did it because I decided to use https://github.com/2amigos/yii2-app-template which imposes .env. It is mostly ok, however there's one (serious to me) problem: environmental variables are being logged in app logs. So if you put sensitive information to .env (like db password) it then appears in logs in plain text hundreds of times! |
https://diogomonica.com/2017/03/27/why-you-shouldnt-use-env-variables-for-secret-data/ |
@jmper please report as separate issue. |
I'm not sure where to report it. So far, unless I am mistaken, neither Yii nor pre-built basic/advanced app templates use .env files. |
Yii core since it's what logs environment variables. |
I don't think it is a good idea, because IMHO it is not a question of logging. The issue is not that environment shouldn't be logged (it should, it's good for debugging purposes), only one shouldn't put sensitive information into environment at all. |
Well, that's commonly accepted practice nowadays and almost every deploy tool follows it. It's not wise to go against it. It is optional though. Yii core won't have anything about environment variables, it will be part of template. |
That's exactly what I'm saying. I'm not against using .env and evironment. I just mentioned the issue with sensitive information here in th hope that maybe someone has solution ready for it. |
As said before; please do not name it
This should have been addressed a while ago: #6419 - see also https://github.com/dmstr/phd5-app/blob/master/src/config/common.php#L132-L142 |
I find this to be quite a bold statement. It may be true for you but that does not mean, there's a general recommendation to be made. But - please - stop extending your personal concerns even further to other unrelated projects. |
Absolutely! I stated several times, that I do not support the naming I think we can agree on that Docker won't fix that soon.
I just will not ignore the fact that |
@schmunk42 Noted. |
okey but how to be with directive settings like |
I suggest to add
.env
support, like it's done in Laravel. With it it's much easier to make a basicconfig of the app/switching between environments, etc.
The text was updated successfully, but these errors were encountered: