在同一服务器上加载同一项目的不同版本的配置文件的最佳方法是什么?

发布于 2024-07-20 07:06:56 字数 530 浏览 4 评论 0原文

我有一个大型 php 项目,依赖于两个级别的配置文件。

在我的项目文件夹中,我有一个 default.config.ini,它被加载,然后与站点特定的配置文件合并。

目前,代码正在读取指向特定配置文件的环境变量PROJECT_CONFIG。 这对于每个在自己的机器上工作的开发人员来说效果很好。 当我们将项目移动到服务器并希望拥有同一项目的三个实例:Dev、Stage、Live 时,就会出现问题。

我们现在不再可以使用全局环境变量,因为每个子域的环境变量都需要不同(项目设置为 dev.domain.com、stage.domain.com 和 www.domain.com)。

我考虑过将服务器变量 HTTP_HOST 转换为环境变量名称,并使用它来设置正确的配置(即当用户从 dev.domain.com 请求页面时,代码将查找环境变量var 名为 dev_domain_com),但我想看看其他人在做什么以及他们推荐什么。

任何建议将不胜感激,提前致谢

I have a large php project that relies on two levels of config files.

In my project folder I have a default.config.ini that is loaded and then merged with a site specific config file.

At the moment the code is reading an environment variable PROJECT_CONFIG that points to the specific config file. This works fine for each developer working on their own machine. The problem arises when we move the project to the server and want to have three instances of the same project: Dev, Stage, Live.

We now no longer can use the global env var since it needs to be different of each subdomain (project is setup as dev.domain.com, stage.domain.com and www.domain.com).

I have considered converting the server variable HTTP_HOST into an env var name and using that to set the right config (i.e. when a user requested a page from dev.domain.com, the code would look for an env var called dev_domain_com), but I wanted to see what other people are doing and what they recommend.

Any advice would be greatly appreciated, thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兲鉂ぱ嘚淚 2024-07-27 07:06:56

使用 apache 的 SetEnv 指令来设置您的 PROJECT_CONFIG< /code> 在容器中配置对应用程序实例的访问:

SetEnv PROJECT_CONFIG /src/dev/app.config.php

Use apache's SetEnv directive to set your PROJECT_CONFIG in the container configuring access to the application instance:

SetEnv PROJECT_CONFIG /src/dev/app.config.php
2024-07-27 07:06:56

大卫·施密特的想法是最优雅的。 我可以看到使用 $_SERVER['HTTP_HOST'] 来确定您所在的服务器并相应地设置路径的情况。 一种这样的情况可能是您没有修改服务器虚拟主机配置文件的权限。 例如:

<?php
  switch($_SERVER['HTTP_HOST']){
    case 'dev.domain.com':
      $path = '/home/dev_user/project.config.ini';
      break;
    case 'stage.domain.com':
      $path = '/home/stage_user/project.config.ini';
      break;
    case 'www.domain.com':
      $path = '/home/live_user/project.config.ini';
      break;
    default:
      $path = '';
  }
  if(!defined('PROJECT_CONFIG')){
    define('PROJECT_CONFIG', $path);
  }
  unset($path);
?>

注意:根据您的服务器配置,您可能需要将 $_SERVER['HTTP_HOST'] 替换为 $_SERVER['SERVER_NAME']

David Schmitt's idea is the most elegant. I could see a case for using $_SERVER['HTTP_HOST'] to determine which server you're on and set the path accordingly. One such case could be if you do not have privileges to modify the server's virtual host configuration files. For instance:

<?php
  switch($_SERVER['HTTP_HOST']){
    case 'dev.domain.com':
      $path = '/home/dev_user/project.config.ini';
      break;
    case 'stage.domain.com':
      $path = '/home/stage_user/project.config.ini';
      break;
    case 'www.domain.com':
      $path = '/home/live_user/project.config.ini';
      break;
    default:
      $path = '';
  }
  if(!defined('PROJECT_CONFIG')){
    define('PROJECT_CONFIG', $path);
  }
  unset($path);
?>

Note: Depending on your server configuration you may want to replace $_SERVER['HTTP_HOST'] with $_SERVER['SERVER_NAME'].

圈圈圆圆圈圈 2024-07-27 07:06:56

我认为你当前的解决方案很好。 环境设置毕竟是有原因的。 我已经看到你的做事方式用于我曾经工作过的大型专有cms。

我需要做同样的事情,让开发/阶段/生产站点加载不同的配置并计划使用环境设置,并让它检测主机名作为后备(如果未设置环境设置)。

I think your current solution is just fine. The environment settings are there for a reason after all. I've seen your way of doing things used for a large scale proprietary cms I used to work on.

I need to do the same thing with having dev/stage/production sites that load different configurations and plan to use environment settings and have it detect the hostname as a fallback if the env setting isn't set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文