In Symfony2, where is the right place to store the application parameter?

I would like to save a few specific application values, for example:

  • default identifier number for specific user selection, if not already set
  • keys / tokens / secrets for various API services like facebook or flickr

The closest I have 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 this:

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

they must be available in different packages as well as in templates

+4
source share
1 answer

they must be available in different packages as well as in templates

They are. While you can access the container, you can access the parameters. From the documents you are associated with: $container->getParameter('acme_hello.email.from');

I think there is an error in your parameters.ini example. 'flickr' and 'app' must not be enclosed in brackets. In addition, the first element of parameters.ini should be [parameters] .

Personally, I like to use the app.yml file because I use it in Symfony 1.x projects (and because I see no reason to use the .ini file). You can create app/config/app.yml and import it into your app/config/config.yml as follows:

 imports: - { resource: app.yml } 

Your app.yml will 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 so you get access to the data: $container->getParameter('flickr.callbackUrl');

The third option is to define your parameters directly in app/config/config.yml . The code will be exactly the same as my example for app/config/app.yml . I do not recommend doing this because app/config/config.yml can be populated with package configuration parameters, and I think it is cleaner to have your own application settings in a separate file. But, of course, it all depends on you.

+6
source

All Articles