Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6d8add

Browse files
committed
feature #17920 [DependencyInjection] AsAlias attribute (SzymonKaminski)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [DependencyInjection] `AsAlias` attribute This PR aims to resolve #17916. Commits ------- f3c3168 [DependencyInjection] `AsAlias` attribute
2 parents e9cef15 + f3c3168 commit f6d8add

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

service_container/alias_private.rst

Lines changed: 37 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,25 @@ 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+
// 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+
158195
Deprecating Service Aliases
159196
~~~~~~~~~~~~~~~~~~~~~~~~~~~
160197

0 commit comments

Comments
 (0)