Skip to content

[Console] Commands auto-registration is deprecated #8269

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
22 changes: 18 additions & 4 deletions console.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use it to create your own commands.
Creating a Command
------------------

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

For example, a command called ``CreateUser`` must follow this structure::
For example, you may want a command to create an user::

// src/AppBundle/Command/CreateUserCommand.php
namespace AppBundle\Command;
Expand Down Expand Up @@ -64,12 +64,26 @@ method. Then you can optionally define a help message and the
Executing the Command
---------------------

After configuring the command, you can execute it in the terminal:
Symfony registers any PHP class extending from :class:`Symfony\\Component\\Console\\Command\\Command`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from

as a console command automatically, so you can now execute this command in the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] automatically. So you [...]

terminal:

.. code-block:: terminal

$ php bin/console app:create-user

.. note::

If you're using the :ref:`default services.yml configuration <service-container-services-load-example>`,
your command classes are automatically registered as services.

You can also manually register your command as a service by configuring the service
and :doc:`tagging it </service_container/tags>` with ``console.command``.

Otherwise, Symfony looks in the ``Command/`` directory of bundles for commands
non registered as a service but this is deprecated since Symfony 3.4 and
won't be supported in Symfony 4.0.
Copy link
Member

@xabbuh xabbuh Aug 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move this whole paragraph into a caution block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


As you might expect, this command will do nothing as you didn't write any logic
yet. Add your own logic inside the ``execute()`` method, which has access to the
input stream (e.g. options and arguments) and the output stream (to write
Expand Down
10 changes: 7 additions & 3 deletions console/commands_as_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ How to Define Commands as Services
==================================

If you're using the :ref:`default services.yml configuration <service-container-services-load-example>`,
your command classes are already registered as services. Great! This is the recommended
setup, but it's not required. Symfony also looks in the ``Command/`` directory of
each bundle and automatically registers those classes as commands.
your command classes are already registered as services. Great! This is the
recommended setup.

Symfony also looks in the ``Command/`` directory of each bundle for commands
non registered as a service and automatically registers those classes as
commands. However this auto-registration was deprecated in Symfony 3.4. In
Symfony 4.0, commands won't be auto-registered anymore.

.. note::

Expand Down