如何在 Symfony 中创建自定义 yaml 配置文件
我想要做的非常简单:将数据存储在我稍后想要读取的自定义配置文件中。
我创建了文件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要修改index.php,这很脏!
只需将此行添加到您的 app/frontend/config/frontendConfiguration.class.php
(使用您自己的应用程序名称进行调整)
Do not modify the index.php this is dirty!
Juste add this line to your app/frontend/config/frontendConfiguration.class.php
(adapt with your own app name)
这真的很简单,但也有点棘手:
创建文件
/config/config_handlers.yml
并添加以下内容:然后将这两行添加到
/web/index.php
在... getApplicationConfiguration()
之后(并将它们添加到 frontend_dev.php 以及您希望此配置文件可用的任何位置):所以您的
/web/index.php
之后可能看起来像这样:顺便说一句:这也在您引用的文档中,尽管 checkConfig() 调用位于不同的位置。查找以下内容:“当您需要基于 map.yml 文件并由应用程序中的 myMapConfigHandler 处理程序生成的代码时,请调用以下行:”
玩得开心;-)
It's really easy, but also a little bit hacky:
Create the file
/config/config_handlers.yml
and add this: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):So your
/web/index.php
might look like this afterwards: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 ;-)
如果您为插件执行此操作,则需要在initialize() 方法中加载配置文件。您仍然可以在插件的配置目录中使用 config_handlers.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.
The plugin's config initialize() method is called automatically by sfProjectConfiguration class and all appConfiguration classes (trough inheritance).
如果您的缓存配置文件为空,您可能忘记在 yml 文件中设置环境。
喜欢:
if your cached config-file is empty, you have probably forgotten to set the environment in your yml-file.
like:
适用于所有应用程序文件:
然后您可以使用:
Works in all application files:
Then you can use:
您清除缓存文件了吗?
在生产环境中,所有配置文件、类等都被缓存。
Have you cleared your cache files?
In prodution environment all config files, classes, etc... are being cached.