J. Bruni
J. Bruni

Reputation: 20492

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?

Upvotes: 12

Views: 12157

Answers (3)

rohitcopyright
rohitcopyright

Reputation: 342

You should use parameters.yml for that purpose. and then you can easily get the things in your controller like this

$parameters = $this->container->getParameter('devices');

Upvotes: 0

Vitalii Zurian
Vitalii Zurian

Reputation: 17976

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

Upvotes: 3

Emii Khaos
Emii Khaos

Reputation: 10085

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);

Upvotes: 12

Related Questions