Yii2 中环境文件夹背后的想法是什么以及如何使用它? [英] What the idea behind environment folders in Yii2 and how to use it?

查看:26
本文介绍了Yii2 中环境文件夹背后的想法是什么以及如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多次阅读 Yii2 文档.我也用谷歌搜索,但我找不到任何有用的东西.

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. 我可以在 Git 中使用分支(用于开发、暂存和生产)
  2. *-local.conf 文件会被 Git 忽略,并且无论如何都不会推送到暂存或生产环境
  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

为什么我必须在 environment/dev 中复制所有控制器、视图和其他文件 &环境/产品?

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?

环境文件夹的部署过程是什么?每次将更改推送到生产服务器后,我是否应该调用 init?

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

推荐答案

除非您有非常特殊的需要,否则您很可能会忽略环境文件夹.

You will most likely ignore the environments folder unless you have a very specific need to do otherwise.

您的所有代码都应放入 commonfrontendconsolebackend 文件夹中.common appart,这些是应用程序的默认可用入口点,您将在其中放置控制器逻辑.您显然不必全部使用它们,根据您的特定需求,仅使用 frontend 就足够了.

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.

但是如果你选择高级模板,它可能会使用一个组合......比如,commonbackendfrontend

But then again if you chose the advanced template it's probably to use a combination.. like say, common, backend and frontend

环境文件夹对应于您在运行 ./init 时拥有的选项.即命名:

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

  • 0) 开发
  • 1) 生产

它们包含在您运行 ./init 命令时编辑和/或添加的所有文件.这些包括 VCS (git) 忽略(因此从未创建)的所有文件.

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).

我们讨论的是诸如 *-local.php 之类的文件,出于显而易见的原因,这些文件永远不应进行版本控制.而且入口脚本也会根据您正在初始化的环境而变化.例如,您希望在生产中调试和注销,但在开发中打开.这些是你不能在配置文件级别设置的,因为它们需要在 Yii 应用程序模型之前设置,或者你只知道每次环境初始化时都需要默认设置.

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.

您可以想象添加另一个名为预生产的环境,例如,除了启用日志记录外,它会像生产环境一样初始化您的应用程序.为此,您需要复制 environments/prod 文件夹,根据需要修改入口脚本,并在 environments/index.php 中添加选项.

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.

./init 只需要在你克隆分支后运行一次.如果您对 CI 很感兴趣,那么您的 CI 服务器可能需要在每次运行时运行 ./init 脚本.这可能取决于您如何配置它.如果您对要应用的环境文件夹进行了更改,则需要再次运行它.

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.

  • common :包含所有应用程序通用的逻辑,从配置文件到模型
  • 前端:与前端 Web 界面相关的所有内容,也可以拥有自己的模型等.
  • 后端:与上述相同,但允许在前端和后端应用程序之间使用单独的逻辑.
  • console : 使用 ./yii controller/action
  • 通过命令行访问你的应用
  • 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.

这篇关于Yii2 中环境文件夹背后的想法是什么以及如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆