[英]In Symfony2 where is the correct place to store an app wide parameter?
I'd like to store a few application specific values for example: 我想存储一些特定于应用程序的值,例如:
Closest I've found so far is http://symfony.com/doc/2.0/cookbook/bundles/best_practices.html#configuration 我到目前为止找到的最近的是http://symfony.com/doc/2.0/cookbook/bundles/best_practices.html#configuration
If I used app/config/parameters.ini
it would look like: 如果我使用
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
these should be available in different bundles and also in templates 这些应该在不同的包中以及模板中提供
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');
从您链接到的文档:
$container->getParameter('acme_hello.email.from');
I think there's an error in your parameters.ini
example. 我认为你的
parameters.ini
示例中存在错误。 'flickr' and 'app' shouldn't be wrapped in brackets. 'flickr'和'app'不应该用括号括起来。 Also, the first element of parameters.ini should be
[parameters]
. 此外,parameters.ini的第一个元素应该是
[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.). 就个人而言,我喜欢使用
app.yml
文件,因为我习惯在Symfony 1.x项目中使用它(因为我没有看到使用.ini文件的原因。)。 You can create app/config/app.yml
and import it into your app/config/config.yml
file like this: 您可以创建
app/config/app.yml
并将其导入app/config/config.yml
文件,如下所示:
imports:
- { resource: app.yml }
Your app.yml would look like this: 你的app.yml看起来像这样:
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');
这就是你访问数据的方式:
$container->getParameter('flickr.callbackUrl');
A third option is to define your parameters directly in app/config/config.yml
. 第三种选择是直接在
app/config/config.yml
定义参数。 The code would be exactly the same as my example for app/config/app.yml
. 代码与
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. 我不建议这样做,因为
app/config/config.yml
可以充满捆绑配置参数,我认为将你自己的app params保存在一个单独的文件中会更清晰。 But of course, it's all up to you. 但当然,这完全取决于你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.