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

发布于 2024-08-26 22:19:24 字数 843 浏览 9 评论 0原文

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

我创建了文件 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(),但我想以更好的方式做事。

What I want to do is quite simple: store data in a custom config file that I want to read later on.

I created my file something.yml that I put in the global config directory.
It looks like that:

prod:
  test:  ok

dev:
  test: ko

all:
  foo:  bar
  john: doe

Then I copied the config_handlers.yml and also put it in the config directory and added the following at the top of the file:

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

But if I'm calling sfConfig::get("something_foo"); I keep getting NULL.

What did I do wrong?
I just want to read values, so no need to create a custome config handler, right?

I've read the doc here: http://www.symfony-project.org/book/1_2/19-Mastering-Symfony-s-Configuration-Files even though I'm running 1.4 (I don't think that changed since then).

Edit: Of course I can use sfYaml::load() but I'd like to do things in a better way.

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

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

发布评论

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

评论(6

夜未央樱花落 2024-09-02 22:19:24

不要修改index.php,这很脏!

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

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

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

Do not modify the index.php this is dirty!

Juste add this line to your app/frontend/config/frontendConfiguration.class.php

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

(adapt with your own app name)

森林迷了鹿 2024-09-02 22:19:24

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

创建文件 /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 处理程序生成的代码时,请调用以下行:”

玩得开心;-)

It's really easy, but also a little bit hacky:

Create the file /config/config_handlers.yml and add this:

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

Then add these two lines to /web/index.php after ... getApplicationConfiguration() (and also add them to frontend_dev.php and wherever you want this config file to be available):

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

So your /web/index.php might look like this afterwards:

<?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();

Btw: This is also in the documentation you cited, although the checkConfig() call is in a different place. Look for this: "When you need the code based on the map.yml file and generated by the myMapConfigHandler handler in your application, call the following line:"

Have fun ;-)

欢烬 2024-09-02 22:19:24

如果您为插件执行此操作,则需要在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'));
    }
  }
}

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

If you're doing this for a plugin you need to load the configuration file in the initialize() method. You can still use config_handlers.yml in your plugin's config directory or let the plugin load the handler too.

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

The plugin's config initialize() method is called automatically by sfProjectConfiguration class and all appConfiguration classes (trough inheritance).

豆芽 2024-09-02 22:19:24

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

喜欢:

all:
  test:  value1
  test2: value2

dev:
  test2: value3

if your cached config-file is empty, you have probably forgotten to set the environment in your yml-file.

like:

all:
  test:  value1
  test2: value2

dev:
  test2: value3
国粹 2024-09-02 22:19:24

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

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

然后您可以使用:

sfConfig::get("something_foo");

Works in all application files:

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

Then you can use:

sfConfig::get("something_foo");
万人眼中万个我 2024-09-02 22:19:24

您清除缓存文件了吗?

php symfony cc

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

Have you cleared your cache files?

php symfony cc

In prodution environment all config files, classes, etc... are being cached.

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