Skip to content
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

Closed
Denis4yk opened this issue Feb 17, 2017 · 28 comments
Closed

Add .env file support #13602

Denis4yk opened this issue Feb 17, 2017 · 28 comments

Comments

@Denis4yk
Copy link

Denis4yk commented Feb 17, 2017

I suggest to add .env support, like it's done in Laravel. With it it's much easier to make a basic
config of the app/switching between environments, etc.

@dynasource
Copy link
Member

I suggest to add .env support, like it's done in Laravel. With it it's much easier to make a basic
config of the app/switching between environments, etc.

can you elaborate on this further please? Can you include a link & explanation about the easier part

@Denis4yk
Copy link
Author

Denis4yk commented Feb 17, 2017

@dynasource Of course. Just a quick example.

We want to move from production to test state.
For that we could comment or uncomment that lines in <public_folder>/index.php

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

Than I want to change DB configuration, etc.
I have to do quite easy manipulations, but in different files.

I think, that all environmental things could be stored in .env file. This is a Laravel approach and it looks very comfort for working in teams. This file should contain only variables, that will be different from system to system.

Example of Laravel .env file, that is located at the root folder of the project.

APP_ENV=local
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=password
DB_PASSWORD=
...

It could be much easier enter one file, set up your .env and start working. Also you could have many .env files, like prouction.env, local.env to switch between them in 1 line and one env.example under git to have en example of keys that you need to fill in.

@mikehaertl
Copy link
Contributor

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.

@leandrogehlen
Copy link
Contributor

leandrogehlen commented Feb 17, 2017

I have done something like that. I did a simple change in web/index.php and yii files:

//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 yii\base\Module::$params.

I am not using more the params property, because, i think better to use the enviroment files like @mikehaertl have suggested.

@Denis4yk
Copy link
Author

Denis4yk commented Feb 17, 2017

@mikehaertl
@leandrogehlen

It's not only for fast switching. The main problem, is that app templates could become quite big. .env file is not for setting all the properties of the app, but for setting environmental properties for working in big team. A new member don't have to look through all the configs, he has only to open the .env file and change all the variables there on his own and be sure, that his environmental configuration is done.

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 :)

@dynasource
Copy link
Member

@Denis4yk, thanks for explaining.

Having a env.php in the root definately makes sense to me. A year ago I already started using it in my projects. It's not that it has big impact or anything, but I thought it was the right place to put this information from a logical perspective.

@mikehaertl
Copy link
Contributor

Please keep in mind, that people already use .env files in their projects. So whatever is done here - it should not interfere with projects where .env is already "occupied". Still think, that moving this to the base/advanced templates makes more sense.

@zelenin
Copy link
Contributor

zelenin commented Feb 20, 2017

https://github.com/symfony/dotenv
https://12factor.net/config

@mikehaertl
Copy link
Contributor

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 init() and get().

We load it from our index.php like this:

<?php
require('/var/www/html/helpers/DockerEnv.php');
\DockerEnv::init();
$config = \DockerEnv::webConfig();
(new yii\web\Application($config))->run();

and configure the app in config/web.php like:

'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 index.php and decide if they want to use that mechanism or not.

@dynasource
Copy link
Member

Please keep in mind, that people already use .env files in their projects. So whatever is done here - it should not interfere with projects where .env is already "occupied". Still think, that moving this to the base/advanced templates makes more sense.

agree. Moved to yiisoft/yii2-app-basic#108

@mikehaertl
Copy link
Contributor

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');

@samdark
Copy link
Member

samdark commented Feb 28, 2017

Looks handy. Could be announced at http://yiifeed.com/

@santilin
Copy link
Contributor

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.

@cebe
Copy link
Member

cebe commented Oct 19, 2017

its not going to be part of the framework, if there will be anything official, it would be part of an application template.

@lav45
Copy link
Contributor

lav45 commented Nov 2, 2017

https://github.com/lav45/yii2-project-configuration

  • As storage you can use not only the file but also redis
  • Have a console controller for easy control very useful when you deploy your application

@jmper
Copy link

jmper commented Nov 10, 2017

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!
I didn't yet find a remedy to this, and the app is still in development stage so for now I just put it aside. At some point I will have to face it though.

@SOHELAHMED7
Copy link
Contributor

@samdark
Copy link
Member

samdark commented Nov 10, 2017

@jmper please report as separate issue.

@jmper
Copy link

jmper commented Nov 10, 2017

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.

@samdark
Copy link
Member

samdark commented Nov 10, 2017

Yii core since it's what logs environment variables.

@jmper
Copy link

jmper commented Nov 10, 2017

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.

@samdark
Copy link
Member

samdark commented Nov 10, 2017

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.

@jmper
Copy link

jmper commented Nov 10, 2017

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.

@schmunk42
Copy link
Contributor

As said before; please do not name it .env in the project root by default, due to possible conflicts with docker-compose - see docker/compose#745 (comment)

however there's one (serious to me) problem: environmental variables are being logged in app logs.

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

@mikehaertl
Copy link
Contributor

As said before; please do not name it .env in the project root by default, due to possible conflicts with docker-compose - see docker/compose#745 (comment)

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. .env files are not a docker invention and may already be used in some projects. This again clearly shows, that docker should fix it. But as stated in that issue I don't want to discuss this any further as all arguments are already repeated a 1000 times.

But - please - stop extending your personal concerns even further to other unrelated projects.

@schmunk42
Copy link
Contributor

This again clearly shows, that docker should fix it.

Absolutely! I stated several times, that I do not support the naming .env.
But if an application template should be able to support docker-compose, we should not mix "control" and "application" environment definitions from the beginning, just to avoid problems.
Related: yiisoft/yii-base-web#1 (comment)

I think we can agree on that Docker won't fix that soon.

But - please - stop extending your personal concerns even further to other unrelated projects.

I just will not ignore the fact that docker-compose uses this file and you can do nothing about it.

@samdark
Copy link
Member

samdark commented Nov 10, 2017

@schmunk42 Noted.

@pironmind
Copy link

I have done something like that. I did a simple change in web/index.php and yii files:

//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 yii\base\Module::$params.

I am not using more the params property, because, i think better to use the enviroment files like @mikehaertl have suggested.

okey but how to be with directive settings like ini_set('display_errors', x);?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests