CakePHP 中的不同环境?

发布于 2024-12-29 07:43:40 字数 124 浏览 1 评论 0原文

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

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

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

I wanna have many environments in my CakePHP application,

and have a core.php file for each environment, i.e.,

core-production.php and core-development.php. How to manage it?

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

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

发布评论

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

评论(1

流年已逝 2025-01-05 07:43:41

如果我理解正确的话,您希望为每个位置加载不同的配置。管理此问题的最佳方法是建立自定义配置基于服务器的位置。

为此,您可以创建一个 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 核心读取文件。

快乐编码!

If I understand correctly, you are looking to load different configurations for each location. The best way to manage this is to establish custom configurations based on the location of the server.

So to do this, you can create a custom.php configuration that checks the server name.

$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;
}

Now, in the core, you can configure the settings based on your environment:

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;
}

Now you can configure everything anywhere using Configure::write('MyDomain.environment', x) without having to modify the way the CakePHP core reads the files.

Happy coding!

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