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.
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
@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.
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
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.
@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 :)
@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.
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.
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.
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 https://github.com/yiisoft/yii2-app-basic/issues/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!
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.
https://diogomonica.com/2017/03/27/why-you-shouldnt-use-env-variables-for-secret-data/
One should also go through these.
@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 .env
in the project root by default, due to possible conflicts with docker-compose
- see https://github.com/docker/compose/issues/745#issuecomment-282812287
however there's one (serious to me) problem: environmental variables are being logged in app logs.
This should have been addressed a while ago: https://github.com/yiisoft/yii2/issues/6419 - see also https://github.com/dmstr/phd5-app/blob/master/src/config/common.php#L132-L142
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.
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: https://github.com/yiisoft/yii2-app/issues/1#issuecomment-343045913
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.
@schmunk42 Noted.
I have done something like that. I did a simple change in
web/index.php
andyii
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);
?
Most helpful comment
@dynasource Of course. Just a quick example.
We want to move from
production
totest
state.For that we could comment or uncomment that lines in
<public_folder>/index.php
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.It could be much easier enter one file, set up your
.env
and start working. Also you could have many.env
files, likeprouction.env
,local.env
to switch between them in 1 line and oneenv.example
undergit
to have en example of keys that you need to fill in.