如何在Symfony中创建自定义的yaml配置文件

19

我想做的事情非常简单:将数据存储在自定义配置文件中,以便稍后读取。

我创建了一个名为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(),但我希望以更好的方式完成事情。


出于好奇,您会使用自定义配置文件做什么,与现有的配置文件功能有何不同?如果只是简单的数据,为什么不能使用app.yml?或者/lib/中的自定义类? - Tom
app.yml会很好用。但是我们在谈论大量数据。我只想保持事情分开。 - Guillaume Flandre
我了解到......维护YAML格式相当严格,所以个人认为存储数据不应该选择这个路线,但我想你肯定有你的理由。 - Tom
@Tom:Yaml 文件非常好用,就像数组一样。我也喜欢 XML,但在将它们用作配置设置时,我认为 Yaml 文件要干净得多且更易于使用。 - Mike Purcell
6个回答

12

不要修改index.php,这是不好的!

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

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

(根据您自己的应用程序名称进行调整)


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类(通过继承)自动调用。


我确信这是一种很好的方式来包含(自定义)yml文件,尽管我有一个 sfProjectConfiguration 而不是 sfApplicationConfiguration。您是否知道如何以这种方式包含它? - LaVomit

5

这很简单,但有点巧妙:

创建文件/config/config_handlers.yml并添加以下内容:

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

/web/index.php中,在... 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处理程序生成的代码在应用程序中时,请调用以下行:”祝玩得开心;-)

1
如果您的缓存配置文件为空,那么您很可能忘记在yml文件中设置环境变量。
例如:
all:
  test:  value1
  test2: value2

dev:
  test2: value3

1

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

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

然后您可以使用:

sfConfig::get("something_foo");

0
你清空了缓存文件吗?
php symfony cc

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接