在 Symfony2 中,存储应用程序范围参数的正确位置在哪里?
我想存储一些应用程序特定的值,例如:
- 特定用户选择的默认 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
。只要能访问容器,就可以访问参数。从您链接到的文档:
$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
文件中,如下所示:您的 app.yml 如下所示
:这是您访问数据的方式:
$container->getParameter('flickr.callbackUrl');
第三种选择是直接在
app/config/config.yml
中定义参数。该代码与我的app/config/app.yml
示例完全相同。不过,我不建议这样做,因为app/config/config.yml
可能会充满捆绑配置参数,而且我认为将自己的应用程序参数保存在单独的文件中会更干净。当然,这一切都取决于你。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 createapp/config/app.yml
and import it into yourapp/config/config.yml
file like this:Your app.yml would look like this:
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 forapp/config/app.yml
. I don't recommend doing this though becauseapp/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.