【问题标题】:different environments in CakePHP?CakePHP 中的不同环境?
【发布时间】:2012-01-26 19:06:27
【问题描述】:

我想在我的 CakePHP 应用程序中有很多环境,

并为每个环境都有一个 core.php 文件,即,

core-production.php 和 core-development.php。如何管理?

【问题讨论】:

  • 生产文件和开发文件有什么区别?我只是从同一个 core.php 管理它们。
  • @Wylie 它将自动选择配置值,从而减少出错的机会和减少单调的劳动。

标签: development-environment cakephp-2.0 production-environment


【解决方案1】:

如果我理解正确,您希望为每个位置加载不同的配置。管理此问题的最佳方法是根据服务器的位置建立custom configurations

为此,您可以创建一个用于检查服务器名称的 custom.php 配置。

$domain = strtolower(@$_SERVER['SERVER_NAME']);
switch (true) {
  default:
  case 'production.domain.com' == $domain:
    Configure::write('MyDomain.environment', 'production');
    break;

  case 'staging.domain.com' == $domain:
    Configure::write('MyDomain.environment', 'staging');
    break;

  case 'local.domain.com' == $domain:
  case 'mybox.com' == $domain:
    Configure::write('MyDomain.environment', 'local');
    break;
}

现在,在核心中,您可以根据您的环境配置设置:

switch (Configure::read('MyDomain.environment')) {
  default: // for security; wouldn't want any confusion revealing sensitive information
    case 'production':
    Configure::write('debug', 0);
    break;

  case 'staging':
  case 'local':
    Configure::write('debug', 2);
    break;
}

现在您可以使用Configure::write('MyDomain.environment', x) 在任何地方配置所有内容,而无需修改 CakePHP 核心读取文件的方式。

编码愉快!

【讨论】:

  • 这是我见过的最好的打开字符串的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多