如何在 Symfony 中创建自定义 yaml 配置文件 [英] How to create a custom yaml config file in Symfony

查看:28
本文介绍了如何在 Symfony 中创建自定义 yaml 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我创建了我的文件 something.yml,并将其放在全局 config 目录中.看起来是这样的:

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

然后我把 config_handlers.yml 复制下来,放到 config 目录下,在文件顶部添加如下内容:

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_

但如果我调用 sfConfig::get("something_foo"); 我会不断收到 NULL.

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?

我在这里阅读了文档:http://www.symfony-project.org/book/1_2/19-Mastering-Symfony-s-Configuration-Files 即使我运行的是 1.4(我认为从那时起就没有改变).

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).

当然我可以使用 sfYaml::load() 但我想以更好的方式做事.

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

推荐答案

不要修改 index.php 这很脏!

Do not modify the index.php this is dirty!

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

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)

这篇关于如何在 Symfony 中创建自定义 yaml 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆