10000 Merge branch '6.0' into 6.1 · symfony/symfony-docs@eaf27f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit eaf27f7

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: [Console] Document AsCommand attribute
2 parents c3ec6ba + ef97649 commit eaf27f7

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

console.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,28 @@ available in the ``configure()`` method::
132132
Registering the Command
133133
-----------------------
134134

135-
Symfony commands must be registered as services and :doc:`tagged </service_container/tags>`
136-
with the ``console.command`` tag. If you're using the
135+
In PHP 8 and newer versions, you can register the command by adding the
136+
``AsCommand`` attribute to it::
137+
138+
// src/Command/CreateUserCommand.php
139+
namespace App\Command;
140+
141+
use Symfony\Component\Console\Attribute\AsCommand;
142+
use Symfony\Component\Console\Command\Command;
143+
144+
#[AsCommand(
145+
name: 'app:create-user',
146+
description: 'Creates a new user.',
147+
hidden: false,
148+
aliases: ['app:add-user']
149+
)]
150+
class CreateUserCommand extends Command
151+
{
152+
// ...
153+
}
154+
155+
If you can't use PHP attributes, register the command as a service and
156+
:doc:`tag it </service_container/tags>` with the ``console.command`` tag. If you're using the
137157
:ref:`default services.yaml configuration <service-container-services-load-example>`,
138158
this is already done for you, thanks to :ref:`autoconfiguration <services-autoconfigure>`.
139159

0 commit comments

Comments
 (0)
0