如何从 YAML 获取并使用关联数组来执行 Symfony 中的操作?

发布于 2024-09-27 21:51:50 字数 417 浏览 0 评论 0原文

我在 app.yml 中获取了一些配置数据,我想在操作中 foreach 它们。我尝试通过 sfConfig::get('app_datas') 获取它们来执行此操作,但失败了。让我们详细展示它们:

YAML:

all:
  datas:
    foo: bar
    foo2: bar2

在 actions.class.php 中我尝试使用此代码:

foreach (sfConfig::get('app_datas') as $key => $value) {

    echo "key $key has value $value";

}

它不起作用,因为 sfConfig::get('app_datas') 为 NULL,如何简单地获取它?

Ive got in app.yml some configration data, and I want to foreach them in action. I try do this by get them by sfConfig::get('app_datas') but it fails. Lets show them in details:

YAML:

all:
  datas:
    foo: bar
    foo2: bar2

and in the actions.class.php I try use this code:

foreach (sfConfig::get('app_datas') as $key => $value) {

    echo "key $key has value $value";

}

it doesnt work because sfConfig::get('app_datas') is NULL, how simly get it?

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

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

发布评论

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

评论(2

¢蛋碎的人ぎ生 2024-10-04 21:51:50

如果您想以数组形式访问第一级,您可以在中间引入虚拟级别,就像@jeremy 建议的那样。 如果您不希望它实际出现在配置变量名称中,请在其前面加上点

all:
  .baz:
    datas:
      foo: bar
      foo2: bar2

现在您应该能够通过以下方式访问您的数据:

foreach (sfConfig::get('app_datas') as $key => $value) 
{
  echo "key $key has value $value";
}

If you want to access first level as an array you can introduce dummy level in between, just like @jeremy suggested. Prefix it with a dot if you don't want it to actually appear in config the variable names:

all:
  .baz:
    datas:
      foo: bar
      foo2: bar2

Now you should be able to access your data with:

foreach (sfConfig::get('app_datas') as $key => $value) 
{
  echo "key $key has value $value";
}
执笏见 2024-10-04 21:51:50

当 Symfony 加载 app.yml 配置文件时,它只存储第二层。因此您无法直接访问app_datas。如果您想获取包含 foofoo2 的数组,请创建一个 YAML 文件,如下所示:

all:
  datas:
    baz:
      foo: bar
      foo2: bar2

然后您可以执行 sfConfig::get('app_datas_baz') 这将是一个包含 foofoo2 作为键的数组。

编辑时:库巴的方式比假人好;忘记你可以做到这一点。

When Symfony loads app.yml config files, it only stores the 2nd level down. So you can't access app_datas directly. If you want to get an array containing foo and foo2, make a YAML file like:

all:
  datas:
    baz:
      foo: bar
      foo2: bar2

You can then do sfConfig::get('app_datas_baz') which will be an array containing foo and foo2 as keys.

On Edit: kuba's way is better than a dummy; forgot you could do that.

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