Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

If you want to define some config parameters in DependencyInjection folder then you can follow example below. This is often used when developing a new vendor bundle that doesn't have a app folder to contain configurations files like config.yml or parameters.yml so on. Assuming that we have a Football application which is main project and we also have a Team vendor bundle.

For more information, see this, this, this, this and this.


Football config.yml


If you defined some parameters in Football bundle and need to use them in configuration of your Team vendor bundle then the node name in config.yml of Football must match the name of the tree builder of Team vendor bundle. We're going to access parameters below in Team bundle later.


# football/app/config/config.yml
application_backend: # This is the name which matches the name in config.php of bundle
name: inanzzz
domain: inanzzz.com
my_message: Hello

Configuration.php


Since we want to use config.yml parameters above in Team vendor bundle, we must register them first.


namespace TeamBundle\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('application_backend');

$rootNode
->children()
->scalarNode('name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('domain')->isRequired()->cannotBeEmpty()->end()
->scalarNode('my_message')->isRequired()->cannotBeEmpty()->end()
->end();

return $treeBuilder;
}
}

TeamBundleExtension.php


As you can see below, prependSpecialConfigDependentExtension() depends on the values defined in main application while prependSpecialConfigIndependentExtension() has nothing to do with the main application.


namespace TeamBundle\DependencyInjection;

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

class ApplicationBackendExtension extends Extension implements PrependExtensionInterface
{
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'));
foreach (['services.yml', 'controllers.yml', 'repositories.yml', 'factories.yml', 'listeners.yml'] as $service) {
$loader->load($service);
}
}

public function prepend(ContainerBuilder $container)
{
$configs = $container->getExtensionConfig($this->getAlias());

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$this->prependSpecialConfigDependentExtension($container, $config);
$this->prependSpecialConfigIndependentExtension($container);
}

/**
* As you can see this access config parameters of parent application
*/
private function prependSpecialConfigDependentExtension(ContainerBuilder $container, array $config)
{
$container->prependExtensionConfig(
'my_internal_config_extension',
[
'level_one' =>
[
'level_two' => [
'my_name' => $config['name']
],
'my_domain' => $config['domain']
],
'my_special_message' => $config['my_message'],
'another' => 'hello'
]
);
}

/**
* As you can see this has nothing to do with parent application
*/
private function prependSpecialConfigIndependentExtension(ContainerBuilder $container)
{
$container->prependExtensionConfig(
'my_independent_config_extension',
[
'level_one' =>
[
'level_two' => [
'my_name' => 'inanzzz'
],
'my_domain' => 'inanzzz.com'
],
'my_special_message' => 'Welcome',
'another' => 'Back'
]
);
}
}