Symfony2 Configuration for own bundles

From WikiOD

This is a description how you can create configuration for you own bundle in the /app/config/config.{yml,xml}

Create configuration in the app/config/config.yml[edit | edit source]

    url: ''
    client_id: 'test_client_1'
    client_secret: 'test_secret'

This is a basic example for create configuration in yml format, for following the yml format you can take deeper configuration.

Set the config in the created bundle[edit | edit source]

For instance you have a bundle, which generated by the symfony console. In this case in the DependencyInjection/Configuration.php you have to insert your configuration representation:

$treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('amazingservice');


Basically in the DependencyInjection/AmazingserviceExtension.php you will see the following lines:

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

It is not enough for getting the configuration in the Srevices. You have to take it into the container.


In this case the config in the container, so if your Service getting the container as a constructor parameter:

        class: Base\AmazingBundle\Services\AmazingServices
        arguments: [@service_container]

Then you can get the configuration in the service with the following code, where the configuration will be an associative array:

private $config;

public function __construct(Container $container){
  $this->config = $container->getParameter('amazingservice.config');