Symfony News

New in Symfony 5.1: Simpler service decoration

Nicolas Grekas

Contributed by
Nicolas Grekas
in #36389.

Service decoration is used in Symfony applications to change the behavior of some service without replacing it entirely. To do that, you need to inject the original service as an argument of the new decorating service. The problem is that the original service no longer exists, so you can't use its original ID.

In previous Symfony versions you needed to use the syntax decorating service ID + .inner to refer to that service. This quickly becomes cumbersome in YAML/XML when using PHP classes as service IDs. That's why in Symfony 5.1 we've simplified this feature to always use .inner to refer to the original service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# config/services.yaml
services:
    App\Mailer: ~

    # Before
    App\SpecialMailer:
        decorates: App\Mailer
        arguments: ['@App\SpecialMailer.inner']

    # After
    App\SpecialMailer:
        decorates: App\Mailer
        arguments: ['@.inner']

Sponsor the Symfony project.


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