Mitchel Verschoof
Mitchel Verschoof

Reputation: 1508

Symfony2 treeBuilder new root item

I have created a new root in my treeBuilder:

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $apiNode = $treeBuilder->root('nieuwname');
        $apiNode
            ->children()
                ->scalarNode('test')
                ->isRequired()
            ->end();
    }
}

Then I get the next exception:

The child node "test" at path "nieuwname" must be configured.

Oké sounds good.

# config.yml
nieuwname:
    test: "test"

And then I get a exception:

There is no extension able to load the configuration for "nieuwname"

What do I do wrong?

Upvotes: 1

Views: 113

Answers (2)

Adam Elsodaney
Adam Elsodaney

Reputation: 7808

You'll need to override the Extension alias with what you have defined as your root tree.

namespace Your\BundleNameBundle\DependencyInjection;

class YourBundleNameExtension implements ExtensionInterface
{
    /**
     * {@inheritDoc}
     */
    public function getAlias()
    {
        return 'nieuwname';
    }
}

or change the root tree name to match the alias naming convention, which tends to be an inflection of your bundle name to lowercase and underscores.

So a bundle called MyWebsiteBundle would have the alias my_website

$apiNode = $treeBuilder->root('your_bundle_name');

Upvotes: 1

Spomky-Labs
Spomky-Labs

Reputation: 16695

It is because the alias in your extension class is not exactly the same. Symfony2 detects the alias using the naming convention (e.g. a bundle named MyCustomAPIBundle will have my_custom_a_p_i alias). If you change it in your configuration class, you will have such an exception.

Look at this article to know you to use a custom alias and avoid repeated code lines.

Upvotes: 0

Related Questions