Symfony 4 - 3rd-party bundle commands are no longer automatically discovered

Ben*_*Ben 4 symfony symfony-console symfony-flex symfony4

According to the documentation, a command class must extend Command or ContainerAwareCommand to be automatically discovered and registered (provided its bundle is registered in the Kernel, of course).

Since Symfony 4+, this discovery doesn't work as expected.

Try this:

composer create-project symfony/skeleton test-maxmind-sf4
cd test-maxmind-sf4
composer req cravler/maxmind-geoip-bundle dev-master
php bin/console list
Run Code Online (Sandbox Code Playgroud)

You will notice that:

  • cravler:maxmind:geoip-update is not registered (nothing under a "cravler" namespace
  • Cravler\MaxMindGeoIpBundle\Command\UpdateDatabaseCommand extends ContainerAwareCommand
  • Bundle Cravler\MaxMindGeoIpBundle\CravlerMaxMindGeoIpBundle is registered in config/bundles.php for all environments (auto-generated recipe)

Now when I do exactly the same thing with Symfony 3, everything works properly.

composer create-project symfony/skeleton test-maxmind-sf3 ^3.0
cd test-maxmind-sf3
composer req cravler/maxmind-geoip-bundle dev-master
php bin/console list
Run Code Online (Sandbox Code Playgroud)

What's missing there?

Thank you,

Ben

Mat*_*teo 6

从这里的UPGRADE FROM to 4.0指南:

不再支持依赖基于约定的命令发现。请改用基于 PSR-4 的服务发现。

前:

# app/config/services.yml
services:
    # ...

    # implicit registration of all commands in the `Command` folder
Run Code Online (Sandbox Code Playgroud)

后:

# app/config/services.yml
services:
    # ...

    # explicit commands registration
    AppBundle\Command\:
        resource: '../../src/AppBundle/Command/*'
        tags: ['console.command']
Run Code Online (Sandbox Code Playgroud)

希望这有帮助

  • Symfony 文档刚刚更新以消除这一点的歧义。:-) (2认同)