3:25 PM
0
A: How to switch from environment with dotenv in yii 2?

Michal HynčicaFunction dirname() takes path as first argument. It returns path to parent directory to the given path. You can optionally supply the second argument $levels, but it must be int and it represents how many levels up the dirname() should go before returning the result. The __DIR__ magic constant co...

 
But anyway. How to switch then from environment? I mean if you want to bring it to production. You have to edit every time the index.php file?
 
You have to change it in every entry point. For web server the entry point is web/index.php. For command line like php yii serve the entry point is yii file in project root. The code in my answer is example based on code you've posted and you have to modify paths to match your real file structure.
 
#Michal. Oke, your example works. But only for dev. How to change then the test env file. I have what you mentioned in the post a file env/test/.env in the root folder
And yes, web.php is in the config folder.
For example I read this: yiiframework.com/doc/guide/2.0/en/concept-configurations but I don't understand this at all. And in the terminal you have to do something like: php yii serve dev, php yii serve test... otherwise you have to change every time the index.php file? Ah, oke. So I have read that you have to commit the index.php file and then put the file in .gitignore. So that you don't have to change each time the file.
 
In second part I've added in my edit there is an example how can you deal with different configs without need to change index.php file. The problem is that you will always need some way to tell your app which config should be loaded. It might be by changing index.php, by changing .env file, or by setting $_ENV['YII_ENV'] variable in different way (eg. SetEnv directive in .htaccess).
Commiting index.php to git and then adding it to .gitignore file won't work. That's because once the file was added to git's index it will always watch for its changes even if it's in .gitignore. You have to use skip-worktree flag instead. And you have to set that flag everywhere where you clone your repository.
 
But how to change this: $dotEnv = Dotenv\Dotenv::createImmutable($path . '/env/' . $_ENV['YII_ENV']); if you want to use the test folder? I mean this doesn't work: $dotEnv = Dotenv\Dotenv::createImmutable($path . '/env/test' . $_ENV['YII_ENV']);
HI
 
3:26 PM
Hello
 
So it works now like the way you described. I have an .env file in the root with body:
YII_DEBUG=1
YII_ENV=dev
 
If you followed my example and created global .env file and then environment dependent .env files then you can set YII_ENV variable in global .env file.
 
and I have a folder in the root env/dev and env/test
 
In your testing environment, you can edit the global .env file and change the line to YII_ENV=test.
 
oke, clear. so you don't have to do some in the command line?
 
3:31 PM
It depends... If you are developing and testing your app in same place then you might not want to edit your .env file every time you want to run tests, right?
 
the idea is to have three different environments: dev, test, prod. So three different servers
 
In that case simply don't commit global .env file into git and create it with different YII_ENV=... in each place.
 
Yes, oke. Thank you for your time and effort :)
 
I'm just curious, but... wouldn't it be easier to simply create something like one .env.example file and then copy it into .env and edit as needed instead of loading different .env files?
The whole point of .env file is to avoid commiting config file into git in first place.
 
But then you need every time edit the properteis. Because there three different db's with different urls
 
3:40 PM
Yes, but you've said that each environment is in different server. So you have .env.example in git, when you are setting up your environment you copy it into .env file and set the proper db url for the environment. Since the .env file is in .gitignore it won't be commited or overwritten, so you only need to configure it once in each place.
 
oke, that can be. yes. But I also have to dockerize the app. But that doesn't matter for the .env file is it?
it is by me dinner time. Maybe we can keep in touch. My email is: nengelen@online.nl. And again thank you.
 
Don't worry, the discussion will stay here, so you can read it anytime after your dinner.
If you are using docker-compose.yml you can set up environment variables there. (see docs.docker.com/compose/environment-variables). If the environment variables already exist loading the .env file with Dotenv::createImmutable() won't overwrite them.
In that case you might want to use $dotEnv->safeLoad() instead of $dotEnv->load(). Method load() will throw an exception when .env file is not found but safeLoad() won't.