Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a Symfony 2 bundle configuration from outside the bundle?

Please, don't link to How to expose a Semantic Configuration for a Bundle

In fact, I already have a fully working bundle, with many configuration options. The bundle is actually configured at app/config.yml, on its own section. I have already implemented a ConfigurationInterface, with its TreeBuilder, and so on. And I am able to successfully inject the config in the bundle and use it in the bundle code.

Yet, what I want to do is extremely simple, but even though I have a fully working bundle published and installable using Composer, I've been playing with Symfony 2 only for a few weeks, and probably the answer is indeed ridiculously simple... but I don't know it!

How can I access the bundle configuration from my app controller?

For example... being this the config at app/config.yml (where "devices" has array prototype):

my_bundle:
    format: standard
    devices:
        main:
            color: yellow
            capacity: 200

How can I access these values from the controller of an app using the bundle?

like image 390
J. Bruni Avatar asked Sep 06 '13 12:09

J. Bruni


2 Answers

The whole config is exposed in the parameter paggy_boleto.config as an nested array. To access it, in controller:

$config = $this->container->getParameter('paggy_boleto.config');

var_dump the config to see, how you access the entries in it.

If you need access others bundle config you have to take a look in the bundles Extension class, how they expose the config into the di container. Some bundle like yours exposes the whole config, some other bundles don't (they expose only specific parameters).

In the PaggyBaletoBundle this is the relevant line:

$container->setParameter('paggy_boleto.config', $config);
like image 54
Emii Khaos Avatar answered Oct 30 '22 08:10

Emii Khaos


It depends how you implemented bundle's configuration builder - it prepends its parameters with a certain prefix.

According to your case it is paggy_boleto.config

like image 41
Vitalii Zurian Avatar answered Oct 30 '22 08:10

Vitalii Zurian