Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: Custom configuration root

Tags:

php

symfony

My application consists of multiple bundles that are named like HelloWorldAdminBundle, HelloWorldUserBundle, HelloWorldDemoBundle. This results in a configuration root like hello_world_demo, hello_world_user and hello_world_demo. I want that the configuration roots of my bundles are helloworld_demo, helloworld_user and helloworld_admin. At that point I have to mention that this is not really a technical problem, but more of an aesthetic problem.

I have tried to implement a custom extension and register it in the Bundle:

public function build(ContainerBuilder $container)
{
    parent::build($container);

    $container->registerExtension(new HelloworldDemoExtension());
}

The extension:

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

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }

    public function getAlias()
    {
        return 'hello_world_demo';
    }
}

and finally the configuration:

...
class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('helloworld_demo');
        ...
        return $treeBuilder;
    }
}

I followed the instructions How to expose a Semantic Configuration for a Bundle, however when I add an item to config.yml I get the following error:

There is no extension able to load the configuration for "helloworld_demo"
like image 221
Florian Eckerstorfer Avatar asked Sep 10 '12 12:09

Florian Eckerstorfer


1 Answers

I found the solution to my problem. It works exactly as I described it in the question, however, a default extension (with the name of the bundle) must exist.

I now have an empty, but existing extension named HelloWorldDemoExtension (with the alias hello_world_demo and additionally I added DemoExtension (with the alias helloworld_demo) and it works.

like image 107
Florian Eckerstorfer Avatar answered Oct 01 '22 16:10

Florian Eckerstorfer