@@ -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,27 @@ 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
+ .. code-block :: php
183
+
184
+ // src/Mail/PhpMailer.php
185
+ namespace App\Mail;
186
+
187
+ // ...
188
+ use Symfony\Component\DependencyInjection\Attribute\AsAlias;
189
+ use Symfony\Component\Mailer\MailerInterface;
190
+
191
+ #[AsAlias()]
192
+ class PhpMailer implements MailerInterface
193
+ {
194
+ // ...
195
+ }
196
+
158
197
Deprecating Service Aliases
159
198
~~~~~~~~~~~~~~~~~~~~~~~~~~~
160
199
0 commit comments