简体   繁体   English

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

[英]How to create a custom yaml config file in Symfony

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. 我创建something.yml我放在全局config目录中的文件something.yml 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_handlers.yml并将其放在config目录中,并在文件顶部添加了以下内容:

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

But if I'm calling sfConfig::get("something_foo"); 但是,如果我正在调用sfConfig::get("something_foo"); I keep getting NULL . 我一直都是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). 我已经阅读了这里的文档: http//www.symfony-project.org/book/1_2/19-Mastering-Symfony-s-Configuration-Files即使我正在运行1.4(我不认为改变了自那时候起)。

Edit: Of course I can use sfYaml::load() but I'd like to do things in a better way. 编辑:当然我可以使用sfYaml::load()但我想以更好的方式做事。

Do not modify the index.php this is dirty! 不要修改index.php这很脏!

Juste add this line to your app/frontend/config/frontendConfiguration.class.php Juste将此行添加到您的app / frontend / config / frontendConfiguration.class.php中

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

(adapt with your own app name) (适应您自己的应用名称)

It's really easy, but also a little bit hacky: 它真的很简单,但也有点hacky:

Create the file /config/config_handlers.yml and add this: 创建文件/config/config_handlers.yml并添加:

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): 然后在... getApplicationConfiguration()之后将这两行添加到/web/index.php (并将它们添加到frontend_dev.php以及您希望此配置文件可用的任何位置):

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

So your /web/index.php might look like this afterwards: 所以你的/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();

Btw: This is also in the documentation you cited, although the checkConfig() call is in a different place. 顺便说一句:这也在你引用的文档中,尽管checkConfig()调用位于不同的地方。 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:" 请查看:“当您需要基于map.yml文件的代码并由应用程序中的myMapConfigHandler处理程序生成时,请调用以下行:”

Have fun ;-) 玩得开心 ;-)

If you're doing this for a plugin you need to load the configuration file in the initialize() method. 如果您正在为插件执行此操作,则需要在initialize()方法中加载配置文件。 You can still use config_handlers.yml in your plugin's config directory or let the plugin load the handler too. 您仍然可以在插件的config目录中使用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'));
    }
  }
}

The plugin's config initialize() method is called automatically by sfProjectConfiguration class and all appConfiguration classes (trough inheritance). 插件的config initialize()方法由sfProjectConfiguration类和所有appConfiguration类(通过继承)自动调用。

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");

if your cached config-file is empty, you have probably forgotten to set the environment in your yml-file. 如果您的缓存配置文件为空,您可能忘记在yml文件中设置环境。

like: 喜欢:

all:
  test:  value1
  test2: value2

dev:
  test2: value3

Have you cleared your cache files? 你清除了缓存文件了吗?

php symfony cc

In prodution environment all config files, classes, etc... are being cached. 在生产环境中,所有配置文件,类等都被缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM