1

I have a bundle that has a listener which I have configured:

class Configuration implements ConfigurationInterface
{

    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder ()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('mybundle_name');

        $rootNode
          ->children()
            ->scalarNode('name')->defaultValue('value')
          ->end()
        ;

        return $treeBuilder;
    }

}

I also have a listener which has a few services injected into, primarily doctrine and the container parameters:

services:
    app.router_subscriber:
        class: MyBundle\EventSubscriber\RequestSubscriber
        calls:
            - [setEntityManager, ['@doctrine.orm.entity_manager']]
            - [setContainer, ['@service_container']]
        tags:
            - { name: kernel.event_subscriber }

When I dump the $this->container I can see the parameters except my own defined above.

When I run

bin/console config:dump-reference MyBundle

I do see what I am expecting

What am I missing to have my bundle parameters get merged into the application parameters? I am seeing third party bundles listed but not my own bundle. I followed the docs as verbatim as I could so the conventions have been followed as far as I am aware...

EDIT | I haven't created a bundle config.yml file - I assumed the Configuraiton object did that for me - setting the schema and default values - which could be overridden by application configs (if desired). Do I need to specifcy a bundle config.yml and import into application something like this (Merge config files in symfony2)?

Ideas?

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
  • Not sure I understand your question. Config values do not automatically become parameters. You need to create them explicitly inside of your Extension::load() method using $container->setParameter(); Of course you might be asking something completely different. – Cerad Aug 05 '17 at 16:15
  • I'm not sure what my question is either TBH. I assumed that Configure::getConfigTreeBuilder() basically setup the format and expected values, default values, etc and that was sufficient for initializing container with defaults - which my application code could then use? – Alex.Barylski Aug 05 '17 at 16:20
  • Basically...how do I initialize my bundle internally (via Configuration class and/or config.yml) and have those parameters available via the service container in AppBundle or the MyBundle respectively. I have followed the docs and I am missing something because those parameters are not showing up. I have failed to registered something or mis understood something – Alex.Barylski Aug 05 '17 at 16:31

1 Answers1

1

I wrote a couple blog posts showing how you can set bundle configuration defaults using YAML files, and a follow-up on how to automatically set bundle configuration values as container parameters. This was for Symfony2 and written in 2014, and the particular section of the Symfony documentation I link to disappeared from Symfony 2.3 onward, but the same concept still applies.

The main takeaway from those posts is that you can set your configuration values as container parameters in your bundle's Extension class via the load() method manually like so:

public function load(array $configs, ContainerBuilder $container)
{
    $configuration = new Configuration();
    $config = $this->processConfiguration($configuration, $configs);

    $container->setParameter($this->getAlias().'.name', $config['name']);
}

Notice that you can call $this->getAlias() to get the bundle's root name (mybundle_name). Using the above call you would then have a parameter defined as mybundle_name.name which you could then override in your application's config.yml if need be.

Jason Roman
  • 8,146
  • 10
  • 35
  • 40
  • Thanks man - I just figured this out but I appreciate you confirming I was on the right track. I wish the docs were a bit more clear in this respect - this had me spinning tires for the last hour :o Also very much like the getAlias() that helps - awesome – Alex.Barylski Aug 05 '17 at 16:47
  • No problem, I don't know why the docs disappeared after 2.2 for some of this which I considered useful...I think it was to simplify the process, as this often applies more to 3rd-party bundles than internal ones in your application. – Jason Roman Aug 05 '17 at 16:51
  • 1
    I am sure they have their reasons...the docs are incredible for the most part but I've been stung a couple times now due to versioning differences. – Alex.Barylski Aug 05 '17 at 16:55