[英]How to change different configuration settings between environments in PHP?
I have aa few php files which I call via AJAX calls. 我有一些通过AJAX调用的php文件。 They all have a URL to my config.php. 它们都有指向我的config.php的URL。 Now I've the problem that I always have to change the URLs to that config file by hand when I deploy a new version on my server. 现在,我遇到的问题是,当我在服务器上部署新版本时,总是必须手动将URL更改为该配置文件。
Local Path: 本地路径:
define('__ROOT__', $_SERVER["DOCUMENT_ROOT"].'/mywebsite');
Server Path: 服务器路径:
define('__ROOT__', $_SERVER["DOCUMENT_ROOT"].'/../dev.my-website.tld/Modules/');
I want to track changes in all of these PHP files. 我想跟踪所有这些PHP文件中的更改。 I'm searching for a solution to automatically change this path. 我正在寻找一种自动更改此路径的解决方案。
Eg This is my current workflow: 例如,这是我当前的工作流程:
Local Environment: 当地环境:
Server: 服务器:
You are trying to run different code bases between development and live, which is not recommended -- they should be identical. 您正在尝试在开发和运行之间运行不同的代码库,不建议这样做-它们应该相同。 The way I tackle this is to use an environment variable to specify which of several config files should be loaded. 我解决此问题的方法是使用环境变量来指定应加载几个配置文件中的哪个。
In my Apache vhost I do something like this: 在我的Apache vhost中,我执行以下操作:
SetEnv ENVIRONMENT_NAME local
And then I use a function to read the environment name: 然后,我使用一个函数来读取环境名称:
function getEnvironmentName()
{
$envKeyName = 'ENVIRONMENT_NAME';
$envName = isset($_SERVER[$envKeyName]) ? $_SERVER[$envKeyName] : null;
if (!$envName)
{
throw new \Exception('No environment name found, cannot proceed');
}
return $envName;
}
That environment name can then be used in a config file to include
, or to retrieve values from a single array keyed on environment. 然后,可以在配置文件中使用该环境名称以include
,或从键入环境的单个数组中检索值。
I often keep environment-specific settings in a folder called configs/
, but you can store them anywhere it makes sense in your app. 我通常将特定于环境的设置保存在名为configs/
的文件夹中,但是您可以将其存储在应用程序中任何有意义的位置。 So for example you could have this file: 因此,例如,您可以拥有以下文件:
// This is /configs/local.php
return array(
'path' => '/mywebsite',
// As many key-values as you want
);
You can then do this (assuming your front controller is one level deep in your project, eg in /web/index.php
): 然后,您可以执行此操作(假设前端控制器在项目中位于一个层次深处,例如/web/index.php
):
$root = dirname(__DIR__);
$config = include($root . '/configs/' . getEnvironmentName() . '.php');
You'll then have access to the appropriate per-environment settings in $config
. 然后,您可以在$config
访问相应的每个环境设置。
A pure git way to achieve this would be filters . 达到此目的的纯git方法将是filter 。 Filters are quite cool but often overlooked. 过滤器很酷,但经常被忽略。 Think of filters as a git way of keyword expansion that you could fully control. 将过滤器视为可以完全控制的git关键字扩展方式。
The checked in version of your file would for example look like this: 例如,文件的签入版本将如下所示:
define('__ROOT__', 'MUST_BE_REPLACED_BY_SMUDGE');
Then set up two filters: 然后设置两个过滤器:
on your local machine, you'd set up a smudge
filter that replaces 在本地计算机上,您将设置一个smudge
过滤器来替换
'MUST_BE_REPLACED_BY_SMUDGE'
with 同
$_SERVER["DOCUMENT_ROOT"].'/mywebsite'
on your server, you'd set up a smudge
filter that replaces 在您的服务器上,您将设置一个smudge
过滤器来替换
'MUST_BE_REPLACED_BY_SMUDGE'
with 同
$_SERVER["DOCUMENT_ROOT"].'/../dev.my-website.tld/Modules/'
on both machines, the clean
filter would restore the line to be 在两台机器上, clean
过滤器都会将线路恢复为
define('__ROOT__', 'MUST_BE_REPLACED_BY_SMUDGE');
Further information about filters could be found in this answer and in the Git Book . 有关过滤器的更多信息,可以在此答案和Git Book中找到 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.