3

I want to create a configuration for my own bundle like this:

my_filters:
    filter_type_a:
        - \MyBundle\My\ClassA
        - \MyBundle\My\ClassA2
    filter_type_b:
        - \MyBundle\My\ClassB

The my_filters should be an array of variable length, and the my_filters.filter_type_a should be an array of variable length,too.

I tried

$treeBuilder = new TreeBuilder(); 
$rootNode = $treeBuilder->root('my_bundle');
$rootNode
     ->children()
         ->arrayNode('my_filters')
             ->prototype('array')
                 ->prototype('array')
                     ->children()
                         ->scalarNode('my_filters')->end()
                      ->end()
                 ->end()
             ->end()
         ->end()
     ->end()

but i got the following error: Invalid type for path "my_bundle.my_filters.filter_type_a.0". Expected array, but got string.

Here is, where i set the configuration:

class MyBundleExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $this->loadServices($container);
        $this->loadConfig($configs, $container);
    }

    private function loadConfig(array $configs, ContainerBuilder $container)
    {       
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        $container->setParameter('my_bundle.my_filters', $config['my_filters']);
    }

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

I can not see my mistake, can anyone tell me?

4
  • What is your rootNode? Change one of your examples to comply each other. Now you have defined filter_expressions but provide my_filters or filter_type_a. It is unclear. Commented May 25, 2015 at 12:55
  • Sorry, forgot that. I've added the root node to the sample. Commented May 25, 2015 at 14:35
  • I think that your mistake in naming. You declare two different nodes with one name my_filters Commented May 25, 2015 at 14:37
  • The error appears, even if i change the name of the inner scalar node. Commented May 25, 2015 at 14:46

1 Answer 1

4

To match config

my_bundle:
    my_filters:
        filter_type_a:
            - \MyBundle\My\ClassA
            - \MyBundle\My\ClassA2
        filter_type_b:
            - \MyBundle\My\ClassB

You need next code in config tree builder:

$rootNode = $treeBuilder->root('my_bundle');
$rootNode
     ->children()
         ->arrayNode('my_filters')
             ->prototype('array')
                 ->prototype('scalar')->end()
             ->end()
         ->end()
     ->end()
Sign up to request clarification or add additional context in comments.

9 Comments

Okay, there is no error, but the parameter 'my_filters' is empty, if i try to read it with $container->getParameter('my_bundle.my_filters').
Ok. Post your code where you set parameter my_bundle.my_filters. I think that problem will be there.
Post the whole class code. Do you sure that your loadConfig is called while container compilation?
Done. Yes, i'm shure.
this answer should be posted in official symfony documentation ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.