Search code examples
symfonysymfony-3.2

Symfony bundle config parameters not available in listener?


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?


Solution

  • 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.