I\'ve read through Yii2 documentation several times. I also googled and I couldn\'t find anything useful yet.
The problem is I do not understand the concept of the envi
You will most likely ignore the environments folder unless you have a very specific need to do otherwise.
All your code should go into either common
, frontend
, console
, or backend
folders. common
appart, these are the default available entry points to your application, where you will place your controller logic. You obviously don't have to use them all, simply using frontend
could suffice depending on your specific need.
But then again if you chose the advanced template it's probably to use a combination.. like say, common
, backend
and frontend
The environment folders correspond to the options you have when running ./init
. That is to name:
They contain all the files that are edited and/or added when you run the ./init
command. These include all the files that are ignored (and therefore never created) by the VCS (git).
We're talking about files like *-local.php files that for obvious reasons should never be versioned. But also the entry scripts that change depending on the environment you are initializing. For example, you want debugging and logging off when in production, but on in development. These are things you can't set up on the configuration file level as they need to be set before the Yii application mock-up or that you just know will need to be default every time the environment is initialized.
You could imagine adding another environment by the name of pre-production for example that would initialize your application exactly like the production environment except with logging enabled. For this you would copy the environments/prod
folder, modify the entry scripts to your needs, and add the option in environments/index.php
.
The ./init
only needs to be run once after you clone the branch. If you're big on CI then your CI server may need to run the ./init
script on every run. This could depend on how you configured it though. You will need to run it again if you've made changes to the environment folders that you want to apply.
This you probably already know but just incase someone was wondering.
./yii controller/action
This is usually where all the magic happens, no need to duplicate any code.
First of all you don't need to put controllers and views in environment folder. environment
folder contains files that contain different configuration for different environments.
For example in frontend/web/index.php
file you would want to set YII_ENV
to prod
while in production environment and to dev
while in development environment. In environment
folder this file is already available with those specific settings in specific folders.
So as explained Here, all you need to do is run the init
command and choose your environment and it will put environment specific files in their proper location.