What the idea behind environment folders in Yii2 and how to use it?

前端 未结 2 2067
Happy的楠姐
Happy的楠姐 2021-02-19 08:53

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

相关标签:
2条回答
  • 2021-02-19 09:05

    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 environments folder

    The environment folders correspond to the options you have when running ./init. That is to name:

    • 0) Development
    • 1) Production

    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.

    common, console and *ends

    This you probably already know but just incase someone was wondering.

    • common : contains logic common to all of your application, from configuration files to models
    • frontend : everything that pertains to your frontend web interface, can also have it's own models etc..
    • backend : same as above but allows for separate logic between frontend and backend application.
    • console : For accessing your app via the command line with ./yii controller/action

    This is usually where all the magic happens, no need to duplicate any code.

    0 讨论(0)
  • 2021-02-19 09:19

    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_ENVto prodwhile in production environment and to devwhile in development environment. In environmentfolder this file is already available with those specific settings in specific folders.

    So as explained Here, all you need to do is run the initcommand and choose your environment and it will put environment specific files in their proper location.

    0 讨论(0)
提交回复
热议问题