4

This is for Symfony 4.

I need to implement application (not bundle), configuration parameters using DependencyInjection\Configuration and DependencyInjection\AppExtension.

I created both classes as I would for a bundle. The namespace for each is App\DependencyInjection.

The file and class names are Configuration and AppExtension, respectively and are located in src/DependencyInjection/.

The Configuration currently just defines two scalar nodes for simplicity.

// src/DependencyInjection/Configuration.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' );
    $rootNode
      ->addDefaultsIfNotSet()
      ->children()
        ->scalarNode( 'a1' )
          ->defaultValue( 'I am the default value for a1' )
        ->end()
        ->scalarNode( 'a2' )
          ->defaultValue( 'I am the default value for a2' )
        ->end()
      ->end();
    return $treeBuilder;
  }    
} 

The AppExtension class implements two methods: __construct() and load().
The __construct() method simply echos its method name.
The load() method currently does a die( __METHOD__ ); just to ensure that I know if it's called. It isn't.

// src/DependencyInjection/AppExtension.php

namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class AppExtension extends Extension
{
  public function __construct()
  {
    echo __METHOD__ . "()\n";
  }

  public function load( array $configs, ContainerBuilder $container )
  {
    die( __METHOD__ );
  }
}

I also created a config file:

# config/app.yml
app:
  a1: 'A one'
  a2: 'A two'

The answer to this question indicates that I need to include a call to $container->registerExtension() in the configureContainer() method of my src/kernel.php file so I added that.

protected function configureContainer( ContainerBuilder $container, LoaderInterface $loader )
{
  $container->registerExtension( new AppExtension() );
  ...
}

When I run the console cache:clear command, the constructor for AppExtension is called, but the load() method never is.

Running the console debug:config app command results in this error:

No extensions with configuration available for "app".

What am I missing?
TIA

6
  • 1
    You will need to register an App Bundle class to get the extension and configuration to work as expected. There used to be some majic associated with the bundle name itself. Not sure how much remains. You will have to experiment a bit with the naming. Of course the whole point of moving to a bundle-less application is to avoid all this stuff. So you might want to sit back and make sure you understand exactly what you are trying to accomplish.
    – Cerad
    Feb 19, 2018 at 22:19
  • I don't mind not having to create a bundle for the application. I can see the gains achieved by removing that requirement and simplifying the src/ tree. What I don't like is not being able to define a hierarchical configuration for my apps. I have apps under Symfony 3 where the app configuration (not bundles), is three to four levels deep (yeah, they're complicated apps dealing with legacy data on "big iron"). So, there is no way to implement a real configuration for the app without an App bundle? If so, that's a real bummer. :-( Feb 19, 2018 at 22:43
  • 1
    To create a bundle you only need one file so I'm not sure I would call it a huge bummer. I'm not really going so far as to say creating a bundle file is the "only" way to implement a real configuration. You do have complete control over the Kernel and it may be possible to override a few methods. Looks like you solved it. Great.
    – Cerad
    Feb 19, 2018 at 23:26
  • @Cerad, Thanks for the hint about having to use a bundle. Turns out to be fairly easy (see my answer). Seems like we shouldn't have to do this though. I can't be the only Symfony developer that occasionally needs complicated app configurations. Feb 19, 2018 at 23:28
  • 1
    I think using a bundle is the correct approach. Both the bundle and the dependency injection extension stuff is all part of the http-kernel component. Using a bundle is how you supposed to extend the container.
    – Cerad
    Feb 19, 2018 at 23:35

2 Answers 2

2

In the initial post there was just one line missing to get it work without creating an extra bundle:

src/DependencyInjection .
-- AppExtension .
-- AppConfiguration

In src/Kernel.php:

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
    //...

    $container->registerExtension(new AppExtension());
    $container->loadFromExtension('app'); //this was missing in initial question

    //...
}
1

So. I have a solution.
It takes three things beyond the normal Configuration and AppExtension classes in src/DependencyInjection/

Implement an empty App\AppBundle class that extends Bundle and place it in src/

// src/AppBundle.php
namespace App;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppBundle extends Bundle {}

Add an entry for the bundle to the array returned by config/bundles.php

<?php
// config/bundles.php
return [
    ...
    App\AppBundle::class => ['all' => true],
]

Place your configuration parameters in config/packages/app.yaml

# config/packages/app.yaml
app:
  a1: 'A1 from app.yaml'
  a2: 'A2 from app.yaml'

  array_one:
    a:  { attr1: 'attribute one', attr2: 'two' }
    b:  { attr1: 'attribute one', attr2: 'two' }

This is actually working for me.
By the way, doing it this way, you do not need to register the extension in Kernel.php.

Your Answer

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

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