18

我想要做的很简单:将数据存储在我想稍后读取的自定义配置文件中。

我创建了something.yml放在全局config目录中的文件。它看起来像这样:

prod:
  test:  ok

dev:
  test: ko

all:
  foo:  bar
  john: doe

然后我复制了 config_handlers.yml 并将其放在 config 目录中,并在文件顶部添加了以下内容:

config/something.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: something_

但是,如果我打电话,sfConfig::get("something_foo");我会不断收到NULL

我做错了什么?我只想读取值,所以不需要创建自定义配置处理程序,对吧?

我在这里阅读了文档:http ://www.symfony-project.org/book/1_2/19-Mastering-Symfony-s-Configuration-Files ,即使我运行的是 1.4(我认为没有改变自那时候起)。

编辑:我当然可以使用sfYaml::load(),但我想以更好的方式做事。

4

6 回答 6

12

不要修改 index.php 这很脏!

只需将此行添加到您的 app/frontend/config/frontendConfiguration.class.php

require_once($this->getConfigCache()->checkConfig('config/something.yml'));

(适应您自己的应用名称)

于 2010-12-16T02:10:24.660 回答
5

这真的很简单,但也有点 hacky:

创建文件/config/config_handlers.yml并添加:

config/something.yml:
  class:    sfDefineEnvironmentConfigHandler
  param:
    prefix: something_

然后将这两行添加到/web/index.phpafter ... getApplicationConfiguration()(并将它们添加到 frontend_dev.php 以及您希望此配置文件可用的任何位置):

$configCache = new sfConfigCache($configuration);
include($configCache->checkConfig('config/something.yml'));

所以你/web/index.php可能看起来像这样:

<?php
require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
$configCache = new sfConfigCache($configuration);
$configCache->checkConfig('config/something.yml');
sfContext::createInstance($configuration)->dispatch();

顺便说一句:这也在您引用的文档中,尽管 checkConfig() 调用位于不同的位置。查找以下内容:“当您需要基于 map.yml 文件并由应用程序中的 myMapConfigHandler 处理程序生成的代码时,请调用以下行:”

玩得开心 ;-)

于 2010-04-06T13:33:28.677 回答
5

如果您为插件执行此操作,则需要在 initialize() 方法中加载配置文件。您仍然可以在插件的配置目录中使用 config_handlers.yml 或让插件也加载处理程序。

class myPluginConfiguration extends sfPluginConfiguration
{
  public function setup() // loads handler if needed
  {
    if ($this->configuration instanceof sfApplicationConfiguration)
    {
      $configCache = $this->configuration->getConfigCache();
      $configCache->registerConfigHandler('config/features.yml', 'sfDefineEnvironmentConfigHandler',
        array('prefix' => 'feature_'));
      $configCache->checkConfig('config/features.yml');
    }
  }

  public function initialize() // loads the actual config file
  {
    if ($this->configuration instanceof sfApplicationConfiguration)
    {
      $configCache = $this->configuration->getConfigCache();
      include($configCache->checkConfig('config/features.yml'));
    }
  }
}

插件的 config initialize() 方法由 sfProjectConfiguration 类和所有 appConfiguration 类(通过继承)自动调用。

于 2011-08-12T14:20:06.317 回答
1

适用于所有应用程序文件:

$configCache = sfApplicationConfiguration::getActive()->getConfigCache();
$configCache->registerConfigHandler('config/something.yml', 'sfDefineEnvironmentConfigHandler', Array('prefix' => 'something_'));
include $configCache->checkConfig('config/something.yml');

然后你可以使用:

sfConfig::get("something_foo");
于 2012-08-28T13:11:18.607 回答
1

如果您缓存的配置文件为空,您可能忘记在 yml 文件中设置环境。

喜欢:

all:
  test:  value1
  test2: value2

dev:
  test2: value3
于 2011-04-07T07:13:16.317 回答
0

您是否清除了缓存文件?

php symfony cc

在生产环境中,所有配置文件、类等都被缓存。

于 2010-04-07T07:50:56.147 回答