Symfony 4 was released on November 30th.
Update now to the best Symfony ever!

New in Symfony 3.1: YAML deprecations

Contributed by
Christian Flothmann
in #17462, #17809 and #17746.

In Symfony 2.8 we introduced some YAML deprecations to make configuration files compliant with the YAML spec. In Symfony 3.1 we introduced additional deprecations, some of them needed for the new YAML features which will be explained in an upcoming article.

Deprecated the !!php/object tag

The values of the YAML properties can be serialized PHP objects thanks to the special !!php/object tag:

1
2
3
# app/config/config.yml
parameters:
    my_object: '!!php/object:O:27:"AppBundle\Service\MyService":1:{s:1:"b";s:3:"foo";}'

In Symfony 3.1 we deprecated the !!php/object tag in favor of the new !php/object tag, which is almost identical but it's prefixed with just one ! character:

1
2
3
# app/config/config.yml
parameters:
    my_object: '!php/object:O:27:"AppBundle\Service\MyService":1:{s:1:"b";s:3:"foo";}'

Deprecated the Dumper::setIndentation() method

When using the Yaml component independently from the Symfony framework, you may have used the setIndentation() method to set the number of white spaces added on each indentation level (which is 4 by default).

1
2
3
4
use Symfony\Component\Yaml\Dumper;

$yaml = new Dumper();
$yaml->setIndentation(2);

In Symfony 3.1 this method is deprecated and it will be removed in Symfony 4.0. Instead, set the indentation level as the constructor argument:

1
2
3
use Symfony\Component\Yaml\Dumper;

$yaml = new Dumper(2);

Deprecated starting scalars with % characters

This deprecation will certainly affect all of your Symfony applications. If you open your YAML configuration files, you'll find some values enclosed with % characters, which means that they are Symfony container parameters:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
framework:
    secret:       %secret%
    router:
        resource: %kernel.root_dir%/config/routing.yml
        # ...
    default_locale:  %locale%

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    # ...

In Symfony 3.1, the usage of % at the beginning of an unquoted string is deprecated and it will be removed in Symfony 4.0. The solution is simple but a bit tiresome: wrap these strings with single or double quotes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
framework:
    secret:       '%secret%'
    router:
        resource: '%kernel.root_dir%/config/routing.yml'
        # ...
    default_locale:  '%locale%'

twig:
    debug:            '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    # ...

Comments

Does parameter wrapping will work correctly for boolean parameters? '%kernel.debug%' will equal as 'false'. Does it convert string to boolean after?
In YAML, "false" as a string is considered a boolean false. In fact, lots of different strings are considered valid boolean values. See http://yaml.org/type/bool.html
But as it is stated in the symfony doc, if it is surrounded with quotes (like in this case), it will be treated as string.

Quote from http://symfony.com/doc/current/components/yaml/yaml_format.html

Finally, there are other cases when the strings must be quoted, no matter if you're using single or double quotes:

When the string is true or false (otherwise, it would be treated as a boolean value);
I'm afraid that no. See example from ruby https://repl.it/CHH7/0
in one of the yaml code block, the syntax highlighting is missing!
@Oskar the highlighting is missing ... because the YAML syntax is wrong. Why? Because strings which start with "%" are not wrapped with quotes. And that's precisely why we deprecated this.
@Konstantin You are mixing two things here: The YAML parser does not know anything about a boolean value when reaching the %kernel.debug% string (no matter with or without quotes). It's just a string. So the outcome after parsing the YAML config is the same before and after this change to the parser. But, what makes this useful is the fact that the DependencyInjection component does some additional things with the service definitions. One of these things is replacing parameter placeholders (like %kernel.debug%) with their actual value. Though at this stage as there is no difference in the placeholder before and after the change to the parser, it can just act like usual and replace it with the boolean true or false (in case the parameter was set properly).

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.