6

I'm trying to import a yaml configuration file in my App following the documentation provided here http://symfony.com/doc/current/bundles/extension.html but I always have the error message:

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

My file is located here : config/packages/app.yaml and has the following structure :

app:  
    list:  
        model1:  
            prop1: value1
            prop2: value2  
        model2:
            ...

As this is a simple App, all the files are in src/. So I have src/DependencyInjection/AppExtension.php

<?php

namespace App\DependencyInjection;

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

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

And src/DependencyInjection/Configuration.php

<?php

namespace App\DependencyInjection;

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

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

        // Node definition
        $rootNode
            ->children()
                ->arrayNode('list')
                    ->useAttributeAsKey('name')
                    ->requiresAtLeastOneElement()
                    ->prototype('array')
                        ->children()
                            ->requiresAtLeastOneElement()
                            ->prototype('scalar')
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

I'm not able to access my parameters :(
Any idea?

1
  • In S4 there is no longer an AppBundle so this approach won't work. If you really need it for some reason then create an AppBundle like S3 has.
    – Cerad
    Dec 21, 2017 at 20:16

1 Answer 1

14

If you want to load a custom configuration file to process it's parameters using an Extension class (like in Symfony bundle extension but without to create a bundle), to eventually "create" and add one or more of it to the "container" (before it will be compiled) you can register your Extension class manually in the configureContainer method contained in the Kernel.php file:

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
    // to avoid the same error you need to put this line at the top
    // if your file is stored under "$this->getProjectDir().'/config'" directory
    $container->registerExtension(new YourAppExtensionClass());

    // ----- rest of the code
}

Then you can use your params as usual registering a Compiler Pass.

Hope this helps.

3
  • 1
    Have not seen this before but it does indeed work. Bit of documentation on registering extensions outside of bundles: symfony.com/doc/current/components/dependency_injection/…
    – Cerad
    Dec 21, 2017 at 21:43
  • @Cerad thanks for the link (initially I wrote the answer very fastly). Personally I discovered the method looking at the source code of the extended classes (like I usually do to learn how Symfony works internally) :-). PS: feel free to improve (also the grammar) my answer (my english it's not so good).
    – gp_sflover
    Dec 21, 2017 at 22:01
  • Might want to take a look at this question as well: stackoverflow.com/questions/47915952/…
    – Cerad
    Dec 21, 2017 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.