Skip to content

[DependencyInjection] AsAlias attribute #17920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions service_container/alias_private.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ services.

.. configuration-block::

.. code-block:: php-attributes
// src/Mail/PhpMailer.php
namespace App\Mail;
// ...
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
#[AsAlias(id: 'app.mailer', public: true)]
class PhpMailer
{
// ...
}
.. code-block:: yaml
# config/services.yaml
Expand Down Expand Up @@ -139,6 +153,10 @@ services.
$services->alias('app.mailer', PhpMailer::class);
};
.. versionadded:: 6.3

The ``#[AsAlias]`` attribute was introduced in Symfony 6.3.

This means that when using the container directly, you can access the
``PhpMailer`` service by asking for the ``app.mailer`` service like this::

Expand All @@ -155,6 +173,25 @@ This means that when using the container directly, you can access the
# ...
app.mailer: '@App\Mail\PhpMailer'
.. tip::

When using ``#[AsAlias]`` attribute, you may omit passing ``id`` argument
if the class implements exactly one interface. ``MailerInterface`` will be
alias of ``PhpMailer``::

// src/Mail/PhpMailer.php
namespace App\Mail;

// ...
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\Mailer\MailerInterface;

#[AsAlias]
class PhpMailer implements MailerInterface
{
// ...
}

Deprecating Service Aliases
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down