New in Symfony 4.3: Deprecating service aliases
January 28, 2019 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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
- XML
- PHP
1 2 3 4 5 6 7
# config/services.yaml
services:
# ...
app.mailer:
alias: App\Mail\PhpMailer
deprecated: ~
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>
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
- XML
- PHP
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.'
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>
1 2 3 4 5
// ...
$container
->setAlias('app.mailer', 'App\Mail\PhpMailer')
->setDeprecated(true, 'The "%alias_id%" service alias is deprecated.');
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Someone could call this title "click bating" LOL
@Javier, maybe you can rename the post and make a 301 or 302 redirection?
Cheers,