@@ -95,6 +95,20 @@ services.
95
95
96
96
.. configuration-block ::
97
97
98
+ .. code-block :: php-attributes
99
+
100
+ // src/Mail/PhpMailer.php
101
+ namespace App\Mail;
102
+
103
+ // ...
104
+ use Symfony\Component\DependencyInjection\Attribute\AsAlias;
105
+
106
+ #[AsAlias(id: 'app.mailer', public: true)]
107
+ class PhpMailer
108
+ {
109
+ // ...
110
+ }
111
+
98
112
.. code-block :: yaml
99
113
100
114
# config/services.yaml
@@ -139,6 +153,10 @@ services.
139
153
$services->alias('app.mailer', PhpMailer::class);
140
154
};
141
155
156
+ .. versionadded :: 6.3
157
+
158
+ The ``#[AsAlias] `` attribute was introduced in Symfony 6.3.
159
+
142
160
This means that when using the container directly, you can access the
143
161
``PhpMailer `` service by asking for the ``app.mailer `` service like this::
144
162
@@ -155,6 +173,25 @@ This means that when using the container directly, you can access the
155
173
# ...
156
174
app.mailer : ' @App\Mail\PhpMailer'
157
175
176
+ .. tip ::
177
+
178
+ When using ``#[AsAlias] `` attribute, you may omit passing ``id `` argument
179
+ if the class implements exactly one interface. ``MailerInterface `` will be
180
+ alias of ``PhpMailer ``::
181
+
182
+ // src/Mail/PhpMailer.php
183
+ namespace App\Mail;
184
+
185
+ // ...
186
+ use Symfony\Component\DependencyInjection\Attribute\AsAlias;
187
+ use Symfony\Component\Mailer\MailerInterface;
188
+
189
+ #[AsAlias]
190
+ class PhpMailer implements MailerInterface
191
+ {
192
+ // ...
193
+ }
194
+
158
195
Deprecating Service Aliases
159
196
~~~~~~~~~~~~~~~~~~~~~~~~~~~
160
197
0 commit comments