dan
dan

Reputation: 41

Custom config in Symfony2

I develop a little bundle, that provides tag cloud functionality. It should be easy, to include it in other Symfony projects and therefore it needs to be configurable. I discovered 3 pages:

I worked along the examples, but it's obvious, that I miss something, because I get the following error message when I use php app/console config:dump-reference:

[Symfony\Component\Config\Exception\FileLoaderLoadException] There is no extension able to load the configuration for "loew_tag" (in somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml). Looked for namespace "loew_tag", found " ... " in somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml (which is being imported from "somePath/blog/app/config/config.yml").

and

[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for "loew_tag" (in /home/somePath/blog/app/config/../../src/Loew/TagBundle/Resources/config/config.yml). Looked for namespace "loew_tag", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "blog", "fos_user", "debug", "web_profiler", "sensio_distribution"

I work inside a 'blog bundle' and try to access the config data for the 'tag bundle'.

Top of my 'app/config/config.yml':

imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: ../../src/Loew/TagBundle/Resources/config/services.yml }
- { resource: ../../src/Loew/TagBundle/Resources/config/config.yml }

LoewTagExtension.php:

<?php
// Loew/TagBundle/DependencyInjection/LoewTagExtension.php

namespace Loew\TagBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class LoewTagExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        //$container->setParameter('food_entities',     $config['food_entities']);
        $container->setParameter('split_match', $config['split_match']);

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

config.yml:

loew_tag:
#    food_entities:
#     - "BlogBundle:Article"
#     - "BlogBundle:Comment"
    split_match: "/[^0-9a-zA-ZöÖüÜäÄß]/"

Configuration.php:

<?php
// Loew/TagBundle/DependencyInjection/Configuration.php


namespace Loew\TagBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();

        $rootNode = $treeBuilder->root('loew_tag');

        $rootNode
            ->children()
            ->scalarNode('split_match')->end()
//                ->arrayNode('food_entities')
//                ->prototype('scalar')->end()

            ->end();

        return $treeBuilder;
    }
}

Entries for the node food_entities are commented in all files to keep it as simple as possible.

I noticed, that similar questions has been asked and the regarding problems has been solved but I cannot transfer that solutions to this issue.

Any idea, what I do miss?

Upvotes: 4

Views: 954

Answers (1)

dan
dan

Reputation: 41

Finally solved this by

  • keeping naming conventions in mind, especially the part, pointed out by Jakub Zalas
  • removing entry: $loader->load('config.yml'); from the extension file.

Obviously, the config file will be loaded automatically as soon, as the service has been loaded.

Upvotes: 0

Related Questions