在 Symfony2 中,存储应用程序范围参数的正确位置在哪里?

发布于 2024-12-11 12:13:08 字数 648 浏览 0 评论 0原文

我想存储一些应用程序特定的值,例如:

  • 特定用户选择的默认 ID 号(如果尚未设置)
  • 各种服务 API(如 facebook 或 flickr)的密钥/令牌/秘密

到目前为止我发现的最接近的是 < a href="http://symfony.com/doc/2.0/cookbook/bundles/best_practices.html#configuration" rel="nofollow">http://symfony.com/doc/2.0/cookbook/bundles/best_practices.html#configuration

如果我使用 app/config/parameters.ini 它会看起来像:

[flickr]
    callbackUrl = http://example.com/approve
    requestTokenUrl = http://www.flickr.com/services/oauth/request_token
    consumerKey = 123a1237a29b123a5541232e0279123

[app]
    default_layout = 2

这些应该在不同的捆绑包和模板中可用

I'd like to store a few application specific values for example:

  • a default Id number for a particular user choice if it's not set yet
  • keys/tokens/secrets for various services API's like facebook or flickr

Closest I've found so far is http://symfony.com/doc/2.0/cookbook/bundles/best_practices.html#configuration

If I used app/config/parameters.ini it would look like:

[flickr]
    callbackUrl = http://example.com/approve
    requestTokenUrl = http://www.flickr.com/services/oauth/request_token
    consumerKey = 123a1237a29b123a5541232e0279123

[app]
    default_layout = 2

these should be available in different bundles and also in templates

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

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

发布评论

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

评论(1

恬淡成诗 2024-12-18 12:13:08

这些应该在不同的捆绑包和模板中可用

。只要能访问容器,就可以访问参数。从您链接到的文档:
$container->getParameter('acme_hello.email.from');

我认为您的 parameters.ini 示例中有错误。 “flickr”和“app”不应包含在括号中。另外,parameters.ini 的第一个元素应该是[parameters]

就我个人而言,我喜欢使用 app.yml 文件,因为我习惯在 Symfony 1.x 项目中使用它(并且因为我不明白使用 .ini 文件的原因。)。您可以创建 app/config/app.yml 并将其导入到 app/config/config.yml 文件中,如下所示:

imports:
    - { resource: app.yml }

您的 app.yml 如下所示

parameters:
  flickr:
    callbackUrl:     http://example.com/approve
    requestTokenUrl: http://www.flickr.com/services/oauth/request_token
    consumerKey:     123a1237a29b123a5541232e0279123

  app:
    default_layout:  2

:这是您访问数据的方式:
$container->getParameter('flickr.callbackUrl');

第三种选择是直接在 app/config/config.yml 中定义参数。该代码与我的 app/config/app.yml 示例完全相同。不过,我不建议这样做,因为 app/config/config.yml 可能会充满捆绑配置参数,而且我认为将自己的应用程序参数保存在单独的文件中会更干净。当然,这一切都取决于你。

these should be available in different bundles and also in templates

They are. As long as you can access the container, you can access the parameters. From the docs you linked to:
$container->getParameter('acme_hello.email.from');

I think there's an error in your parameters.ini example. 'flickr' and 'app' shouldn't be wrapped in brackets. Also, the first element of parameters.ini should be [parameters].

Personally, I like using an app.yml file because I'm used to using it in Symfony 1.x projects (and because I don't see the reason for using an .ini file.). You can create app/config/app.yml and import it into your app/config/config.yml file like this:

imports:
    - { resource: app.yml }

Your app.yml would look like this:

parameters:
  flickr:
    callbackUrl:     http://example.com/approve
    requestTokenUrl: http://www.flickr.com/services/oauth/request_token
    consumerKey:     123a1237a29b123a5541232e0279123

  app:
    default_layout:  2

And this is how you would access data:
$container->getParameter('flickr.callbackUrl');

A third option is to define your parameters directly in app/config/config.yml. The code would be exactly the same as my example for app/config/app.yml. I don't recommend doing this though because app/config/config.yml can get pretty filled up with bundle configuration parameters, and I think it's cleaner to keep your own app params in a separate file. But of course, it's all up to you.

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