我想要做的非常简单:将数据存储在一个自定义配置文件中,稍后我想要读取该文件。
我创建了放在全局config
目录中的文件something.yml
。看起来是这样的:
prod:
test: ok
dev:
test: ko
all:
foo: bar
john: doe
然后,我复制了配置文件,并将其放入config_handlers.yml目录中,并在文件的顶部添加了以下内容:
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()
,但是我想用一种更好的方式来做事情。
发布于 2010-12-16 10:10:25
不要修改index.php,这是脏的!
只需将此行添加到您的app/frontend/config/frontendConfiguration.class.php
require_once($this->getConfigCache()->checkConfig('config/something.yml'));
(使用您自己的应用程序名称进行调整)
发布于 2010-04-06 21:33:29
这真的很简单,但也有点老生常谈:
创建文件/config/config_handlers.yml
并添加以下内容:
config/something.yml:
class: sfDefineEnvironmentConfigHandler
param:
prefix: something_
然后,将以下两行添加到... getApplicationConfiguration()
之后的/web/index.php
中(并将它们添加到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()调用位于不同的位置。查找如下代码:“当您需要应用程序中由myMapConfigHandler处理程序生成的基于map.yml文件的代码时,调用下面这行代码:”
玩得开心;-)
发布于 2011-08-12 22:20:06
如果要为插件初始化执行此操作,则需要在()方法中加载配置文件。您仍然可以在插件的配置目录中使用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类自动调用(通过继承)。
https://stackoverflow.com/questions/2544553
复制相似问题