English spoken conference

New in Symfony 4.3: Deprecating service aliases

Contributed by Joost van Driel and Renan in #29968.

Deprecations are the key of our backward compatibility promise, which ensures smooth upgrades of your projects between minor versions (e.g. from 4.0 to 4.x). In order to provide a great developer experience, your own Symfony apps can also deprecate services, deprecate config options and even deprecate Twig templates, blocks and macros.

In Symfony 4.3, you'll also be able to deprecate service aliases. When an alias should no longer be used in your application, define it as deprecated with the deprecated configuration option:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    # config/services.yaml
    services:
        # ...
    
        app.mailer:
            alias: App\Mail\PhpMailer
            deprecated: ~
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    <?xml version="1.0" encoding="utf-8"?>
    <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
        <services>
            <!-- ... -->
    
            <service id="app.mailer" alias="App\Mail\PhpMailer">
                <deprecated />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    // ...
    
    $container
        ->setAlias('app.mailer', 'App\Mail\PhpMailer')
        ->setDeprecated(true);
    

If this alias is used anywhere in your app, you'll see a generic error message. If you prefer to customize that message, define it using the deprecated option (the only requirement is that the message must include the %alias_id% placeholder):

  • YAML
    1
    2
    3
    4
    5
    6
    7
    # config/services.yaml
    services:
        # ...
    
        app.mailer:
            alias: App\Mail\PhpMailer
            deprecated: 'The "%alias_id%" service alias is deprecated.'
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    <?xml version="1.0" encoding="utf-8"?>
    <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
        <services>
            <!-- ... -->
    
            <service id="app.mailer" alias="App\Mail\PhpMailer">
                <deprecated>The "%alias_id%" service alias is deprecated.</deprecated>
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    // ...
    
    $container
        ->setAlias('app.mailer', 'App\Mail\PhpMailer')
        ->setDeprecated(true, 'The "%alias_id%" service alias is deprecated.');
    

Comments

The title gave me a scare (thought the aliases themselves were being deprecated), but I love the addition!
@Niels I'm sorry! Now I realize that the title could be confusing :( Sadly I cannot update it now to not break the links to this article.
I join, I also thought at first that they were abandoning aliases.
I thought the same :)

Someone could call this title "click bating" LOL
Same here. This title is going to raise some eyebrows :D
I agree with Niels, it was so scary with the title that needed to read the post to confirm it.

@Javier, maybe you can rename the post and make a 301 or 302 redirection?

Cheers,
reading my rss inbox and reading the title really scared me... xD
The title is a bit misleading. I assumed this will remove aliases completely. Too bad :D
Just as I was trying to figure out how to mark the aliases as deprecated, I read this, hihi.
Also scared in the begining but nice feature !
Nice feature.
I was also scare by the title. But after reading the posts Sounds Nice.

Comments are closed.

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