What the idea behind environment folders in Yii2 a

2019-04-04 07:39发布

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 environment folders. Please let me explain:

  1. I can use branches in Git (for dev, staging and production)
  2. *-local.conf files are ignored by Git and they won't be pushed to staging or production in anyway

Why I have to duplicate all my controllers, views and other files in environment/dev & environment/prod?

In what folders I actually have to make my development?

What the deployment procedure for environment folders? Should I call init every time after I push my changes to production server?

2条回答
放我归山
2楼-- · 2019-04-04 08:25

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.

查看更多
一夜七次
3楼-- · 2019-04-04 08:30

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.

查看更多
登录 后发表回答