Skip to content

Commit 0a9c15c

Browse files
[DependencyInjection] Document AsAlias attribute
1 parent aa033f6 commit 0a9c15c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

service_container/alias_private.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ services.
9595

9696
.. configuration-block::
9797

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+
98112
.. code-block:: yaml
99113
100114
# config/services.yaml
@@ -139,6 +153,10 @@ services.
139153
$services->alias('app.mailer', PhpMailer::class);
140154
};
141155
156+
.. versionadded:: 6.3
157+
158+
The ``#[AsAlias]`` attribute was introduced in Symfony 6.3.
159+
142160
This means that when using the container directly, you can access the
143161
``PhpMailer`` service by asking for the ``app.mailer`` service like this::
144162

@@ -155,6 +173,27 @@ This means that when using the container directly, you can access the
155173
# ...
156174
app.mailer: '@App\Mail\PhpMailer'
157175
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+
158197
Deprecating Service Aliases
159198
~~~~~~~~~~~~~~~~~~~~~~~~~~~
160199

0 commit comments

Comments
 (0)