I also struggled a bit when I begin use the advanced template and environments. But it’s really handy.
First of - you have to realize, that the “init” thing with environments templates and the “-local” files are two different things which “only” overlap in some parts. In other words – “-local” files are always environment dependent but not vice-versa. There are other files which doesn’t contain any secrets but are environment dependent – like index.php and init tool solve these all.
With this info back to your “init” problem.
In many scenarios I would say you only use init when you “create app on new environment”. And this is really important – the more environments, the more use set up these app, the more you have “apps” in you project (like frontend, backend, blog, api, …) the more are environments helpful. So in my case?
- Production server
- Production server but testing version
- Internal testing server
- My local server is also one of the setup!
- And mainly this was new for me – any other person who work in the team and runs his own local machine or even wants to run in on server use the power of environments
Multiply it by 7 apps – common, console, frontend, backend, our-api, app-for-other-api1, app-for-other-api2 a it’s a loooot of configs which are set up for me or any other person in team.
Now to the second part of the “init” question which samdark didn’t fully answer I think – sometime you really use init even if the project is already set up. When and how to handle overwriting the files – this is actually a good question of yours and the creators of yii thought of that.
I use init for already set up project whenever some environment configs are modified. So let’s say I changed few of the index.php files in web folders, changed, what files they are loading (which I actually did) and what environment they are set for(which I actually changed – I added new environment “local”).
In these files, there are no secrets, no empty places which you have to fill when set up the project, but they are environment dependent so that’s why they are part of the environments.
So in this case, I change the environment templates, create commit, then distribute it and am able to run init on any place and my index.php files are updated based on the environment they are (or I chose when running init).
But then you ask – “wouldn’t I overwrite my “-local” configs by this?”. Great question. No – you wont overwrite them. How? You have two options.
Dull – when running the init, the process just asks you for every already existing file, which is about to overwrite. So you can easily chose what to overwrite and what to keep. But this can be very frustrating for many files and many runs.
Clever – you have the check the “index.php” in the root of environments folder which the manuals mentions - Additional Topics: Adding more applications | Yii 2 Advanced Project Template | Yii PHP Framework. It’s very important file and it’s like config for the init process. You can config
- Files which should be skipped if they already exist – this solves the previous problem
- You chose where the init should generate validation keys – very handy
- Files which are writable (permission for the server)
- Files which are executable (permission for the server)
And all those settings can be different for every environment. And it’s really powerful and all those settings are already used in the default index.php. You should definitely check it.
It’s a shame the manual doesn’t say more about it but already is documented in the file.
There is only one scenario which this workflow doesn’t cover – when I want to change one of the “-local” file, for example the app newly uses ftp and needs to add credentials for it, then while running the “init” I’m force to chose to overwrite the file (and then re-add all the info again) or not overwrite (but then manually edit the file). Or I can put these settings to new file, but this isn’t very clean solution.
It’d be great if init could somehow only update the new part of the config. Something like git does. But I guess it’s too complicated. Or clever solution would be use the approach used when generating the validation keys – the init could read the file, store the content, then overwrite the file and re-add the ones which were present in the previous version. Yep, this would be super cool.
But even without it it’s still handy tool.
You also mentioned using init for switching the environments. I actually can’t imagine scenario where you would like to do that. Once the project instance is set up, it serves it’s purposes. Why would you like to for example switch production to develop? I