Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Configuration
  4. Using Parameters within a Dependency Injection Class
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Using Parameters within a Dependency Injection Class

Edit this page

Using Parameters within a Dependency Injection Class

You have seen how to use configuration parameters within Symfony service containers. There are special cases such as when you want, for instance, to use the %kernel.debug% parameter to make the services in your bundle enter debug mode. For this case there is more work to do in order to make the system understand the parameter value. By default, your parameter %kernel.debug% will be treated as a string. Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
// inside Configuration class
$rootNode
    ->children()
        ->booleanNode('logging')->defaultValue('%kernel.debug%')->end()
        // ...
    ->end()
;

// inside the Extension class
$config = $this->processConfiguration($configuration, $configs);
var_dump($config['logging']);

Now, examine the results to see this closely:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
my_bundle:
    logging: true
    # true, as expected

my_bundle:
    logging: '%kernel.debug%'
    # true/false (depends on 2nd argument of the Kernel class),
    # as expected, because %kernel.debug% inside configuration
    # gets evaluated before being passed to the extension

my_bundle: ~
# passes the string "%kernel.debug%".
# Which is always considered as true.
# The Configurator does not know anything about
# "%kernel.debug%" being a parameter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:my-bundle="http://example.org/schema/dic/my_bundle"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://example.org/schema/dic/my_bundle
        https://example.org/schema/dic/my_bundle/my_bundle-1.0.xsd">

    <my-bundle:config logging="true"/>
    <!-- true, as expected -->

    <my-bundle:config logging="%kernel.debug%"/>
    <!-- true/false (depends on 2nd parameter of Kernel),
         as expected, because %kernel.debug% inside configuration
         gets evaluated before being passed to the extension -->

    <my-bundle:config/>
    <!-- passes the string "%kernel.debug%".
         Which is always considered as true.
         The Configurator does not know anything about
         "%kernel.debug%" being a parameter. -->
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$container->loadFromExtension('my_bundle', [
        'logging' => true,
        // true, as expected
    ]
);

$container->loadFromExtension('my_bundle', [
        'logging' => "%kernel.debug%",
        // true/false (depends on 2nd parameter of Kernel),
        // as expected, because %kernel.debug% inside configuration
        // gets evaluated before being passed to the extension
    ]
);

$container->loadFromExtension('my_bundle');
// passes the string "%kernel.debug%".
// Which is always considered as true.
// The Configurator does not know anything about
// "%kernel.debug%" being a parameter.

In order to support this use case, the Configuration class has to be injected with this parameter via the extension as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace App\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    private $debug;

    public function __construct($debug)
    {
        $this->debug = (bool) $debug;
    }

    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('my_bundle');

        $treeBuilder->getRootNode()
            ->children()
                // ...
                ->booleanNode('logging')->defaultValue($this->debug)->end()
                // ...
            ->end()
        ;

        return $treeBuilder;
    }
}

And set it in the constructor of Configuration via the Extension class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace App\DependencyInjection;

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

class AppExtension extends Extension
{
    // ...

    public function getConfiguration(array $config, ContainerBuilder $containerBuilder)
    {
        return new Configuration($containerBuilder->getParameter('kernel.debug'));
    }
}

Tip

There are some instances of %kernel.debug% usage within a Configurator class for example in TwigBundle. However, this is because the default parameter value is set by the Extension class.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    Symfony 6.2 is backed by

    Symfony 6.2 is backed by

    Be safe against critical risks to your projects and businesses

    Be safe against critical risks to your projects and businesses

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Ser5, a Symfony contributor

    Thanks Ser5 for being a Symfony contributor

    2 commits • 4 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia