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

11,237

Solution 1

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

Solution 2

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

Share:
11,237

Related videos on Youtube

J. Bruni
Author by

J. Bruni

Building an ExtJS / PHP CMS

Updated on June 17, 2022

Comments

  • J. Bruni
    J. Bruni almost 2 years

    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?

    • Vitalii Zurian
      Vitalii Zurian over 10 years
      It depends how you implemented bundle's configuration builder - it prepends its parameters with a certain prefix. In your case it might be something like my_bundle.devices. Otherwise, could you post the code of your DependencyInjection/MyBundleExtension.php ?
  • J. Bruni
    J. Bruni over 10 years
    Indeed... @thecatontheflat pointed me to the right direction... and I already got it: $this->container->getParameter('paggy_boleto.config');
  • Emii Khaos
    Emii Khaos over 10 years
    parameters.yml won't work, if you need to access semantic bundle configuration.
  • J. Bruni
    J. Bruni over 10 years
    +1 You really helped me, your comment right into the point made me "discover" the answer before they were posted here. I accepted Pazi's one because he wrote a detailed answer, he has only 2.8k rep while you already have 5k, et cetera. I thank YOU a lot, and I wish I could accept both answers and give you a hundred rep points. A thousand! So, the points go to Pazi, but you have my gratitude and blessings!
  • J. Bruni
    J. Bruni over 10 years
    Thanks! I know it was simple. I've played with Symfony 2 for a week or two, then switched to Laravel + Angular.JS, now back to Symfony 2... too much concurrent threads in the head!