Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

An Aside: Dependency Injection Parameters

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $6.00

An Aside: Dependency Injection Parameters

Cleanse the palette of all the forms stuff and open config.yml. Under the doctrine key, we see a bunch of percent sign values:

# app/config/config.yml
# ...

doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        # ...

Whenever you see something surrounded by two percent signs in a config file, it’s a parameter. Parameters are like variables: you set them somewhere and then use them with this syntax. So where are these being set?

Open up parameters.yml to find the answer:

# app/config/parameters.yml
# ...

# This file is auto-generated during the composer install
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: null
    database_name: knp_events
    database_user: root
    database_password: null
    # ...

In episode 1, we talked about how this file is special because it holds any server-specific configuration. This works because it’s in our .gitignore file so that every developer and server can have their own. So we set parameters here and use them anywhere else.

Adding More Parameters

But technically, we can add parameters to any configuration file. Go back to config.yml and add a new parameters key anywhere in the file. Below it, create a new parameter called our_assets_version, and set it to the assets_version value we’re using below:

# app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: "@EventBundle/Resources/config/services.yml" }
    - { resource: "@UserBundle/Resources/config/services.yml" }

parameters:
    our_assets_version: 5-return-of-the-jedi

framework:
    # ...

Now, just use it under the framework key:

# app/config/config.yml
# ...

framework:
    # ...
    templating:
        engines: ['twig']
        assets_version: %our_assets_version%
        assets_version_format: "%%s?v=%%s"
    # ...

See, they work just like variables. Refresh to make sure we didn’t break anything.

So now you know what these percent signs are all about. Spoiler alert! You can also access parameters from a controller using $this->container->getParameter, which might come in handy.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4", // v2.4.2
        "doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
        "doctrine/doctrine-bundle": "~1.2", // v1.2.0
        "twig/extensions": "~1.0", // v1.0.1
        "symfony/assetic-bundle": "~2.3", // v2.3.0
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.5
        "symfony/monolog-bundle": "~2.4", // v2.5.0
        "sensio/distribution-bundle": "~2.3", // v2.3.4
        "sensio/framework-extra-bundle": "~3.0", // v3.0.0
        "sensio/generator-bundle": "~2.3", // v2.3.4
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "doctrine/doctrine-fixtures-bundle": "~2.2.0", // v2.2.0
        "ircmaxell/password-compat": "~1.0.3", // 1.0.3
        "phpunit/phpunit": "~4.1", // 4.1.0
        "stof/doctrine-extensions-bundle": "~1.1.0" // v1.1.0
    }
}
userVoice