php Yii2:如何准备调试和生产环境?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28852276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:06:06  来源:igfitidea点击:

Yii2: How to prepare for debug and production environment?

phpversion-controlconfigurationyii2web-deployment

提问by robsch

I know Yii defines and uses the constants YII_DEBUGand YII_ENV.Of course, they are set to to 'true' and 'dev' on my local machine. This is because the basic app template has prepared it this way in the index.phpfile. This file tells me also that I should remove those lines for production mode, i.e. on the production machine. Then those constants are set to 'false' and 'prod' by default. That's all okay and I understand it. (More information can be found on Defining Constantsand Environment Constants.)

我知道 Yii 定义并使用了常量YII_DEBUGYII_ENV。当然,它们在我的本地机器上设置为“true”和“dev”。这是因为基本应用程序模板已在index.php文件中以这种方式进行了准备。这个文件还告诉我我应该删除那些用于生产模式的行,即在生产机器上。然后这些常量默认设置为“false”和“prod”。没关系,我理解。(更多信息可以在定义常量环境常量中找到。)

My question: How can I best handle these constants when index.php is contained in VCS?In one environment they should exist, in the other not. And it could be a test machine as well, of course. Which options do I have? I think this is also a matter of the deployment method. Currently, I'm just pushing via Git to the production machine, what is a primitive deployment IMO...

我的问题:当 index.php 包含在 VCS 中时,我怎样才能最好地处理这些常量?在一种环境中它们应该存在,而在另一种环境中则不存在。当然,它也可以是一台测试机。我有哪些选择?我觉得这也是部署方式的问题。目前,我只是通过 Git 推送到生产机器,什么是原始部署 IMO...

How do you do it? What do you suggest?

你怎么做呢?你有什么建议?

EDIT: Actually, handling the params files is the same issue.

编辑:实际上,处理 params 文件是同样的问题。

回答by Chloe

Here's my solution:

这是我的解决方案:

if ($_SERVER['SERVER_NAME'] == 'localhost' || $_SERVER['SERVER_NAME'] == '127.0.0.1') {
  defined('YII_DEBUG') or define('YII_DEBUG', true);
  defined('YII_ENV') or define('YII_ENV', 'dev');
}

Also for Heroku, Setup Yii2 Advanced on Heroku

同样对于 Heroku,在 Heroku 上设置 Yii2 Advanced

回答by Blizz

Yii2 (or at least the advanced application template) has a system of "environments". In this folder you can store the files that change per environment.

Yii2(或者至少是高级应用模板)有一个“ environments”的系统。在此文件夹中,您可以存储随环境变化的文件。

Those files are usually your bootstrap files (index.php) and "local" configuration files (things that override the main configuration).

这些文件通常是您的引导程序文件 ( index.php) 和“本地”配置文件(覆盖主要配置的内容)。

The app template also has an "init" command that allows you to switch.

应用模板还有一个“ init”命令,可以让你切换。

Basically what happens is that you add the entire environments-folder to your VCS, but you ignore the locations where those files are supposed to end up (like Ankit already stated). This way you can keep all different environment-dependant configurations in your VCS next to each other.

基本上发生的情况是您将整个environments-folder添加到 VCS,但您忽略了这些文件应该结束的位置(就像 Ankit 已经说明的那样)。通过这种方式,您可以将 VCS 中的所有不同的环境相关配置彼此相邻。

See herefor a bit more information and herefor an example of how this folder can look.

这里的一个位的更多信息,并在这里为这个文件夹怎么能看一个例子。

回答by robsch

Another simple solution:

另一个简单的解决方案:

File index.php (goes into VCS repo):

文件 index.php(进入 VCS 存储库):

<?php

@include 'my-env.php';

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();

File my-env.php:

文件 my-env.php:

<?php

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

my-env.php won't be added to the VCS. It can exist or not. If not, the app runs automatically in production mode. my-env.php could be also placed into the config folder maybe.

my-env.php 不会添加到 VCS。它可以存在也可以不存在。如果没有,应用程序会在生产模式下自动运行。my-env.php 也可以放在 config 文件夹中。

This is a small improvement compared to Ankit's approach, so that the index.php can be added to the VCS. Hence, the VCS repo contains allrequired files and can be deployed without any manual modifictations.

与 Ankit 的方法相比,这是一个小的改进,因此可以将 index.php 添加到 VCS。因此,VCS 存储库包含所有必需的文件,无需任何手动修改即可部署。

回答by Patryk Radziszewski

Do You have access to config file where you set virtualhosts? If so You can just addsetEnv YII_DEBUG "true"between </Directory>and </VirtualHost>

您是否有权访问设置虚拟主机的配置文件?如果是这样,您可以setEnv YII_DEBUG "true"</Directory>和之间添加</VirtualHost>

回答by ankitr

Commit index.phponce and then add it to .gitignore. So that you do not have to change it every time.

提交index.php一次,然后将其添加到.gitignore。这样您就不必每次都更改它。

Simply add /web/index.phpin .gitignore

只需添加/web/index.php的.gitignore

回答by WeSee

Yii2 works with composer. One question is how to handle composer.lock.

Yii2 与作曲家一起工作。一个问题是如何处理composer.lock

In my Yii2 production sites I put composer.lockto .gitignoreso I can decide on composer updates depending on production issues and keep a production version of composer.lockuntouched during a git pullfor updating production sites.

在我的 Yii2 生产站点composer.lock.gitignore,我可以根据生产问题决定作曲家更新,并composer.lockgit pull更新生产站点期间保持生产版本不变。

My last lines of .gitignore are:

我的 .gitignore 最后几行是:

 # exclude composer.lock from versioning 
 composer.lock