8000 minor #8269 [Console] Commands auto-registration is deprecated (Guilh… · symfony/symfony-docs@a50892c · GitHub
[go: up one dir, main page]

Skip to content

Commit a50892c

Browse files
committed
minor #8269 [Console] Commands auto-registration is deprecated (GuilhemN)
This PR was squashed before being merged into the 3.4 branch (closes #8269). Discussion ---------- [Console] Commands auto-registration is deprecated Document symfony/symfony#23805. I wonder, should I update https://github.com/symfony/symfony-docs/blob/17d1c39298e9415886e2c712748d3021ed7865a4/bundles/best_practices.rst L122 (`Command/ Yes`) ? As it is not really mandatory to use the `Command/` directory anymore. Commits ------- 493ae89 [Console] Commands auto-registration is deprecated
2 parents 556be4d + 493ae89 commit a50892c

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

console.rst

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use it to create your own commands.
1212
Creating a Command
1313
------------------
1414

15-
Commands are defined in classes which must be created in the ``Command`` namespace
16-
of your bundle (e.g. ``AppBundle\Command``) and their names must end with the
15+
Commands are defined in classes which should be created in the ``Command`` namespace
16+
of your bundle (e.g. ``AppBundle\Command``) and their names should end with the
1717
``Command`` suffix.
1818

19-
For example, a command called ``CreateUser`` must follow this structure::
19+
For example, you may want a command to create a user::
2020

2121
// src/AppBundle/Command/CreateUserCommand.php
2222
namespace AppBundle\Command;
@@ -64,12 +64,28 @@ method. Then you can optionally define a help message and the
6464
Executing the Command
6565
---------------------
6666

67-
After configuring the command, you can execute it in the terminal:
67+
Symfony registers any PHP class extending :class:`Symfony\\Component\\Console\\Command\\Command`
68+
as a console command automatically. So you can now execute this command in the
69+
terminal:
6870

6971
.. code-block:: terminal
7072
7173
$ php bin/console app:create-user
7274
75+
.. note::
76+
77+
If you're using the :ref:`default services.yml configuration <service-container-services-load-example>`,
78+
your command classes are automatically registered as services.
79+
80+
You can also manually register your command as a service by configuring the service
81+
and :doc:`tagging it </service_container/tags>` with ``console.command``.
82+
83+
.. caution::
84+
85+
Symfony also looks in the ``Command/`` directory of bundles for commands
86+
non registered as a service but this is deprecated since Symfony 3.4 and
87+
won't be supported in Symfony 4.0.
88+
7389
As you might expect, this command will do nothing as you didn't write any logic
7490
yet. Add your own logic inside the ``execute()`` method, which has access to the
7591
input stream (e.g. options and arguments) and the output stream (to write

console/commands_as_services.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ How to Define Commands as Services
55
==================================
66

77
If you're using the :ref:`default services.yml configuration <service-container-services-load-example>`,
8-
your command classes are already registered as services. Great! This is the recommended
9-
setup, but it's not required. Symfony also looks in the ``Command/`` directory of
10-
each bundle and automatically registers those classes as commands.
8+
your command classes are already registered as services. Great! This is the
9+
recommended setup.
10+
11+
Symfony also looks in the ``Command/`` directory of each bundle for commands
12+
non registered as a service and automatically registers those classes as
13+
commands. However this auto-registration was deprecated in Symfony 3.4. In
14+
Symfony 4.0, commands won't be auto-registered anymore.
1115

1216
.. note::
1317

0 commit comments

Comments
 (0)
0