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

Parameters: The Variables of Configuration

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 $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Congrats! We've basically mastered Symfony configuration and environment system. But there's just one more trick it has up its sleeve.

Look closely inside config.yml file: one of the settings - default_locale - is set to a strange-looking value: %locale%. Huh.

... lines 1 - 10
framework:
... lines 12 - 24
default_locale: "%locale%"
... lines 26 - 79

Scrolling up a bit, there's another root key called parameters with locale: en:

... lines 1 - 5
# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
... lines 10 - 79

You're witnessing the power of a special "variable system" inside config files. Here's how it works: in any of these configuration files, you can have a parameters key. And below that you can create variables like locale and set that to a value. Why is this cool? Because you can then reuse that value in any other file by saying %locale%.

Look under the doctrine key:

... lines 1 - 42
# Doctrine Configuration
doctrine:
dbal:
... line 46
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
... lines 52 - 79

Hey, a bunch more, like %database_host% and %database_port%. These are set just like locale, but in a different file: parameters.yml:

# This file is a "template" of what your parameters.yml file should look like
# Set parameters here that may be different on each deployment target of the app, e.g. development, staging, production.
# http://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration
parameters:
database_host: 127.0.0.1
database_port: ~
database_name: symfony
database_user: root
database_password: ~
... lines 10 - 20

So that's it! If you add a new key under parameters, you can use that in any other file by saying %parameter_name%.

And just like services, we can get a list of every parameter available. How? Ah, our friend the console of course. Run:

./bin/console debug:container --parameters

Woah, that's a huge list you can take advantage of or even override. Most of these you won't care about, but don't forget this little cheatsheet is there for you.

Creating a new Parameter

We can leverage parameters to do something really cool with our cache setup.

In the prod environment, we use the file_system cache. In dev, we use array. We can improve this. Create a new parameter called cache_type and set that to file_system. Scroll down and set type to %cache_type%:

... lines 1 - 7
parameters:
... line 9
cache_type: file_system
... lines 11 - 73
doctrine_cache:
providers:
my_markdown_cache:
type: %cache_type%
... lines 78 - 80

Run over in the terminal to see if the parameter showed up:

./bin/console debug:container --parameters

It's right on top. Cool! Clear the cache in the prod environment so we can double-check everything is still working:

./bin/console cache:clear --env=prod

Ok good - now refresh using app.php. It's still loading fast - so we haven't broken anything... yet.

Here's where things get interesting. In config_dev.yml, it took a lot of code just to turn caching off. Parameters to the rescue! Copy the parameters key from config.yml and paste it into this file. But now, change its value to array and celebrate by completely removing the doctrine_cache key at the bottom:

... lines 1 - 3
parameters:
cache_type: array
... lines 6 - 49

That's it! Refresh the browser in the dev environment: great it's still slow, which means it's working.

Leave a comment!

6
Login or Register to join the conversation
Tom Avatar

So in 'config_dev.yml' it loads in the config.yml file then the parameters are set, how does config.yml read those parameters if they were set after it was imported? Don't parameters have to be set before they can be read? If you understand what i mean

Reply
Default user avatar

Not quoting a scalar starting with the “%” indicator character is deprecated since Symfony 3.1

Reply

You're right - how fast the world moves :). You now need to put quotes around the parameters. But, not quoting them will still be allowed until Symfony 4 (so you have a few years to change them).

Cheers!

Reply
Default user avatar

"%cache_type%" and %cache_type% are both allowed for now right?

Reply

Hey Ruben!

That's correct, your app won't break if you dont add quotes to your parameters, but it's not recommended, the time will arrive and you won't be able to upgrade to Symfony 4 smoothly

Have a nice day!

Reply
Default user avatar

Comment Deleted: Error was unrelated to video content.

Reply
Cat in space

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

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "knplabs/knp-markdown-bundle": "^1.4" // 1.4.2
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.7
        "symfony/phpunit-bridge": "^3.0" // v3.1.3
    }
}
userVoice