How to access nested parameter values in Symfony2?

18,431

Solution 1

In all the symfony config files I've seen, the entries under 'parameters:' have always been fully qualified. I don't fully understand why this is but it may help you to write the entries in your parameters.yml like this:

category1.var1: xxx
category1.var2: yyy
category1.var3. zzz

category2.subcategory1.var1: 5
category2.subcategory1.var2: 10
category2.subcategory2.var1: foo
category2.subcategory2.var2: bar

... and so on.

EDIT

I tried pasting the nested parameter from the question into parameters.local.yml in one of my projects and ran a trivial unit test to retrieve that and a fully qualified parameter from the container e.g.

$testUserEmail = $container->getParameter('test.user.email');
$this->assertEquals('[email protected]', $testUserEmail);
$testParam = $container->getParameter('category.var');
$this->assertEquals('test', $testParam);

The fully qualified parameter was fine, attempting to get the nested parameter resulted in an InvalidArgumentException: The parameter category.var must be defined. I don't think parameters can be defined with nesting.

Solution 2

$this->container->getParameter('category')['var']

I have tested that on symfony 2.8 and it worked for me.

Solution 3

Or you can write simple ParameterBag which can implement nested parameters dynamically (without config values duplicity):

<?php

namespace Your\Namespace;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;

class ParameterBagNested extends ParameterBag
{

    /**
     * wire $this->set() logic into add() too
     *
     * @param array $parameters
     */
    public function add( array $parameters )
    {
        foreach ( $parameters as $name => $value ) {
            $this->set( $name, $value );
        }
    }

    /**
     * sets all levels of nested array parameters with dot notation
     * - loggly[host: loggly.com] will be translated this way:
     *  - loggly: [host: loggly.com] - standard array parameter will be left as is
     *  - loggly.host: loggly.com - nested variables ar translated so you can access them directly too as parent.variable
     *
     * @param string $name
     * @param mixed $value
     */
    public function set( $name, $value )
    {
        if ( $this->has( $name ) ) {
            // this is required because of array values
            // we can have arrays defined there, so we need to remove them first
            // otherwise some subvalues would to remain in the system and as a result, arrays would be merged, not overwriten by set()
            $this->remove( $name );
        }
        $this->setNested( $name, $value );
    }

    /**
     * remove checks even if name is not array
     *
     * @param string $name
     */
    public function remove( $name )
    {
        $value = $this->get( $name );
        if ( is_array( $value ) ) {
            foreach ( $value as $k => $v ) {
                $this->remove( $name . '.' . $k, $v );
            }
        }
        if ( strpos( $name, '.' ) !== FALSE ) {
            $parts = explode( '.', $name );
            $nameTopLevel = reset( $parts );
            array_shift( $parts );
            $topLevelData = $this->removeKeyByAddress( $this->get( $nameTopLevel ), $parts );
            ksort( $topLevelData );
            $this->setNested( $nameTopLevel, $topLevelData );
        }
        parent::remove( $name );
    }

    /**
     * @param array $data
     * @param array $addressParts
     *
     * @return array
     */
    private function removeKeyByAddress( $data, $addressParts )
    {
        $updatedLevel = & $data;
        $i = 1;
        foreach ( $addressParts as $part ) {
            if ( $i === count( $addressParts ) ) {
                unset( $updatedLevel[$part] );
            } else {
                $updatedLevel = & $updatedLevel[$part];
                $i++;
            }
        }
        return $data;
    }

    /**
     * @see set()
     *
     * @param string $name
     * @param mixed $value
     */
    private function setNested( $name, $value )
    {
        if ( is_array( $value ) ) {
            foreach ( $value as $k => $v ) {
                $this->setNested( $name . '.' . $k, $v );
            }
        }
        parent::set( $name, $value );
    }

}

phpunit Test:

<?php

namespace Your\Namespace;

use Symfony\Component\DependencyInjection\Tests\ParameterBag\ParameterBagTest;

/**
 * its essential to use ParameterBagNested as ParameterBag because this way we run even parent class tests upon it
 * parent class is part of Symfony DIC standard test suite and we use it here just for check if our parameter bag is still ok
 */
use SBKS\DependencyInjection\ParameterBag\ParameterBagNested as ParameterBag;

/**
 * testing basic and even added ParameterBag functionality
 */
class ParameterBagNestedTest extends ParameterBagTest
{

    public function testConstructorNested()
    {
        $bag = new ParameterBag(
            array(
                'foo' => array( 'foo1' => 'foo' ),
                'bar' => 'bar',
            )
        );
        $this->assertEquals(
             array(
                 'foo.foo1' => 'foo',
                 'foo' => array(
                     'foo1' => 'foo',
                 ),
                 'bar' => 'bar',
             ),
             $bag->all(),
             '__construct() takes an array of parameters as its first argument'
        );
    }

    public function testRemoveNested()
    {
        $bag = new ParameterBag(
            array(
                'foo' => array(
                    'foo1' => array(
                        'foo11' => 'foo',
                        'foo12' => 'foo',
                    ),
                    'foo2' => 'foo',
                ),
                'bar' => 'bar',
            )
        );

        $bag->remove( 'foo.foo1.foo11' );
        $this->assertEquals(
             array(
                 'foo' => array(
                     'foo1' => array(
                         'foo12' => 'foo',
                     ),
                     'foo2' => 'foo',
                 ),
                 'foo.foo1' => array( 'foo12' => 'foo' ),
                 'foo.foo1.foo12' => 'foo',
                 'foo.foo2' => 'foo',
                 'bar' => 'bar',
             ),
             $bag->all(),
             '->remove() removes a parameter'
        );

        $bag->remove( 'foo' );
        $this->assertEquals(
             array(
                 'bar' => 'bar',
             ),
             $bag->all(),
             '->remove() removes a parameter'
        );
    }

    public function testSetNested()
    {
        $bag = new ParameterBag(
            array(
                'foo' => array(
                    'foo1' => array(
                        'foo11' => 'foo',
                        'foo12' => 'foo',
                    ),
                    'foo2' => 'foo',
                ),
            )
        );
        $bag->set( 'foo', 'foo' );
        $this->assertEquals( array( 'foo' => 'foo' ), $bag->all(), '->set() sets the value of a new parameter' );
    }

    public function testHasNested()
    {
        $bag = new ParameterBag(
            array(
                'foo' => array(
                    'foo1' => array(
                        'foo11' => 'foo',
                        'foo12' => 'foo',
                    ),
                    'foo2' => 'foo',
                ),
            )
        );
        $this->assertTrue( $bag->has( 'foo' ), '->has() returns true if a parameter is defined' );
        $this->assertTrue( $bag->has( 'foo.foo1' ), '->has() returns true if a parameter is defined' );
        $this->assertTrue( $bag->has( 'foo.foo1.foo12' ), '->has() returns true if a parameter is defined' );
        $this->assertTrue( $bag->has( 'foo.foo2' ), '->has() returns true if a parameter is defined' );
    }

}

Then you can use it by injecting Parameter bag into ContainerBuilder:

$parameterBag = new \Your\Namespace\ParameterBagNested();
$container = new ContainerBuilder($parameterBag);

Thats all, you can use nested parameter with dot notation now in Symfony DI container.

If you are using Symfony plugin in phpstorm it will autocomplete your nested attributes with dot notation too.

Solution 4

A bit late, but here is a solution that worked for me.

# app/config/config.yml
twig:
    globals:
        my_var: category['var']

Explanation

When you import your parameters file into your app/config/config.yml file, you can automatically access all variables defined in your parameters file.

Keep in mind, when you use the structure:

# app/config/parameters.yml
parameters:
    category:
        var: test

You are defining a parameter called category with a value that is in turn itself a key-value pair array that contains var as key and test as value.

Update

In newer versions of symfony (3.4 - 4.2) you can now do the following:

# app/config/config.yml
twig:
    globals:
       custom_categories: '%categories%'

By having this parameter set up:

# app/config/parameters.yml
parameters:
    categories:       
      - category_A
      - category_B

Things to note here: - in parameters.yml categories is an array

so to use it in a twig template, you should be able to do something like:

<ul>
{% for category in categories %}
    <li> {{ category }} </li>
{% endfor %}
</ul>

Another example with objects:

# app/config/parameters.yml
parameters:
    marketplace:       
        name: 'My Store'
        address: '...'

Configuring twig variables:

# app/config/config.yml
twig:
    globals:
       marketplace: '%marketplace%'

Using it in twig:

...
<p>{{marketplace.name}}</p>
<p>{{marketplace.address}}</p>
...

Hope this helps! :)

Solution 5

Symfony's expression language syntax to the rescue! Give this a shot:

twig:
    globals:
        my_var: '@=parameter("category")["var"]'
Share:
18,431

Related videos on Youtube

Gottlieb Notschnabel
Author by

Gottlieb Notschnabel

Updated on September 16, 2022

Comments

  • Gottlieb Notschnabel
    Gottlieb Notschnabel over 1 year

    I created a parameter in my parameters.yml file:

    parameters:
        category:
            var: test
    

    How can I access this parameter in my config.yml? For example, I want to pass this parameter to all my twig files as a global twig variable:

    twig:
        globals:
            my_var: %category.var% # throws ParameterNotFoundException
    
  • Gottlieb Notschnabel
    Gottlieb Notschnabel about 10 years
    I like the easyness of keeping settings in the yaml format. So this would just bloat it up in a way that I want to avoid. But thanks for explaining this way.
  • palmic
    palmic about 10 years
    I dont understand. This ParameterBag is just functionality to allow you to use nested configuration in your yaml configurations. Its backward compatible with all your current configurations. Thats all. It does not define php configuration.
  • Serhii Smirnov
    Serhii Smirnov over 8 years
    Where exactly should I inject parameter bag to ContainerBuilder? In Extension classes ContainerBuilder already passed to "load" metod,
  • palmic
    palmic over 8 years
    It depends on your stack, you have to pass it to ContainerBuilder constructor as you can see in my example.
  • Gottlieb Notschnabel
    Gottlieb Notschnabel almost 7 years
    Thanks for your answer. Can you specify the symfony version number (if applicable) since this is possible?
  • Gustavo Iglesias
    Gustavo Iglesias almost 7 years
    I couldn't find to much information about since when this is available, but in my case the need came up in a recent project that I'm working with which is set up with the version 3.2.* of symfony, but I've tested the same solution in a old project using the version: 2.4.*, and it is working as well. So we can conclude that in order to make this work, a project must be set up with the version 2.4.* of symfony.
  • JorgeeFG
    JorgeeFG about 6 years
    I got the literal string category['var'] in the service. Are you sure?. If I use %category['var']% then I get undefined parameter. I'm using Symfony 3.4
  • Charlie Hills
    Charlie Hills over 5 years
    I'm in the same boat. The value of my_var is the literal string "category['var']" and not the value of category.var. I'm on Symfony 4.1 and a bit flummoxed as to how this might work.
  • Gustavo Iglesias
    Gustavo Iglesias over 5 years
    To access parameters, from a custom service the process is different, first you need to make sure you're importing the service container, and from there you can just do something like this: $categories = $this->container->getParameter('category'); echo $categories['var']; output: test. Hope this helps, cheers.
  • Boschman
    Boschman over 5 years
    @Gustavo Iglesias: This is not about a custom service. Your example is about a Twig global variable. And just like JorgeeFG and Charlie Hills I get the literal string category['var']. I'm using Symfony 3.4. Are you sure your example is correct?
  • Boschman
    Boschman over 5 years
    Thanks for updating, but now it doesn't answer the original question anymore. If you look at the original question: how to parse category.var as a Twig global variable, without having to parse the entire category array?
  • CasualBen
    CasualBen over 4 years
    Worked on Symfony 4
  • Martin Melka
    Martin Melka about 2 years
    The downside to this solution is that a missing parameter will not be detected during SF container build but only later on, during runtime.