Symfony News

New in Symfony 3.3: Added new shortcut methods

In addition to some large new features, Symfony 3.3 will also contain minor tweaks to make your work a bit easier.

Added a shortcut to create autowired definitions

Contributed by
Wouter De Jong
in #20648.

Creating service definitions in PHP via the ContainerBuilder work as follows:

1
2
3
4
$container->register('app.twig_extension', AppExtension::class)
    ->setAutowired(true)
    ->addTag('twig.extension')
;

Given that autowiring is all about working more quickly, in Symfony 3.3 you can use the new autowire() shortcut method to achieve the same result:

1
2
3
$container->autowire('app.twig_extension', AppExtension::class)
    ->addTag('twig.extension')
;

Added shorthand methods for Config prototypes

Contributed by
Ilyes kooli
in #20921.

The prototype() method of the ArrayNodeDefinition class allows you to create different types of prototypes for integers, floats, booleans, arrays, etc. The problem is that this method always returns a NodeDefinition object instead of the specific object created (IntegerNodeDefinition, ArrayNodeDefinition).

This makes IDEs unable to understand code like the following, where the max() method is undefined for the NodeDefinition object:

1
2
$node = new ArrayNodeDefinition('name');
$node->prototype('integer')->max(10);

In Symfony 3.3 we added a dedicated shortcut method for each of the possible prototypes. Now you can refactor the previous example as follows and your IDE will recognize the max() method:

1
2
$node = new ArrayNodeDefinition('name');
$node->integerPrototype()->max(10);

Added a Yaml syntax shortcut for name-only tags

Contributed by
Wouter De Jong
in #20651.

Tags used in service definitions can define configuration parameters, but they usually define just their names:

1
2
3
4
5
services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        tags:
            - { name: twig.extension }

In these cases, defining a Yaml hash (- { name: twig.extension }) is overkill. In Symfony 3.3, when only the tag name is needed, you can just add the tag as a string:

1
2
3
4
services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        tags: ['twig.extension']

Be trained by Symfony experts - 2017-01-09 Paris - 2017-01-09 Paris - 2017-01-11 Paris


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook