New in Symfony 3.4: debug:autowiring command

Contributed by
Ryan Weaver
in #24583.

Service autowiring was introduced as an experimental feature in Symfony 2.8. In the following Symfony versions, we improved it so much that we decided to enable it by default in Symfony 3.3.

One of the main features of autowiring is that you can type-hint the arguments of the class constructors or controller methods and Symfony automatically injects the services associated with those type-hinted classes.

This saves you most of the previously needed service configuration, but requires you to know the right type-hint to use. In previous Symfony versions, you could execute the debug:container --types command to get that information. However, in Symfony 3.4 we added a new dedicated debug:autowiring command which is much easier to remember and provides the same information more nicely.

For example, if you run this command in the Symfony Demo application, you'll get the following results:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ ./bin/console debug:autowiring

Autowirable Services
====================

The following classes & interfaces can be used as type-hints when autowiring:

  App\Command\AddUserCommand
  App\Command\DeleteUserCommand
  App\Command\ListUsersCommand
  App\Controller\Admin\BlogController
  App\Controller\BlogController
  App\Controller\SecurityController
  App\EventSubscriber\CheckRequirementsSubscriber
  App\EventSubscriber\CommentNotificationSubscriber
  ...
  Symfony\Component\Validator\Validator\ValidatorInterface
      alias to debug.validator
  Twig\Environment
      alias to twig

You can also provide an argument to filter the list of autowirable services:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ ./bin/console debug:autowiring log

Autowirable Services
====================

 The following classes & interfaces can be used as type-hints when autowiring:
 (only showing classes/interfaces matching log):

  App\Controller\Admin\BlogController
  App\Controller\BlogController
  Psr\Log\LoggerInterface
      alias to monolog.logger

Comments

Great!
Thanks a lot.
Wow. Great feature. Tnx!
Login with SensioLabsConnect to post a comment