Symfony2: Custom Root Configuration

My application consists of several packages, which are called HelloWorldAdminBundle, HelloWorldUserBundle, HelloWorldDemoBundle. This leads to the root of the configuration, for example hello_world_demo, hello_world_userand hello_world_demo. I want the configuration roots of my packages to be helloworld_demo, helloworld_userand helloworld_admin. At this point, I must mention that this is not a real technical problem, but rather an aesthetic problem.

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

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

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

Expansion:

...
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 open a semantic configuration for the Bundle , however, when I add an element to config.yml, I get the following error:

There is no extension able to load the configuration for "helloworld_demo"
+5
source
4

. , , , ( ) .

, HelloWorldDemoExtension ( hello_world_demo , , DemoExtension ( helloworld_demo) .

+3

, Symfony\Component\HttpKernel\Bundle\Bundle#getContainerExtension().

ExtensionInterface, , ( getAlias , Carlos Granados ') .

, getContainerExtension() , LogicException.

:

<?php

namespace HelloWorld\Bundle\UserBundle;

use HelloWorld\Bundle\UserBundle\DependencyInjection\HelloWorldUserExtension;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class HelloWorldUserBundle extends Bundle
{
    public function getContainerExtension()
    {
        if (null === $this->extension) {
            $this->extension = new HelloWorldUserExtension();
        }

        return $this->extension;
    }
}

, , :

/**
 * The extension alias
 *
 * @return string
 */
public function getAlias()
{
    return 'helloworld_user';
}

. , ​​ getContainerExtension(). , , . .

. .: Symfony2: , Bundles. , . , . Symfony, . , , .

+3

:

:

  • DependencyInjection;
  • (AcmeHelloExtension AcmeHelloBundle);
  • XSD.

, build getAlias. , getAlias , node. hello_world_demo

helloworld_demo
0
source

From the book “In this case, the extension class must also implement the getAlias ​​() method and return a unique alias after the package (for example, acme_hello). This is required because the class name does not meet the standards, ending with the extension. In addition, the load () method your extension will only be called if the user specifies the alias acme_hello in at least one configuration file. "

So, this IS alias is the root configuration name. Change your code to

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

And it should work

0
source

All Articles