Symfony 3.4:生成包:ClassNotFoundException:尝试从命名空间 "MyVendorFOSUserBundle" 加载 class "MyVendorFOSUserBundle"

Symfony 3.4 : generate bundle : ClassNotFoundException: Attempted to load class "MyVendorFOSUserBundle" from namespace "MyVendorFOSUserBundle"

大家好,我正在从 FOSUserBundle 继承以覆盖一些控制器,但是在创建简单捆绑包的第一步时,我发现了一个问题:

bin/console genrate:bundle

Are you planning on sharing this bundle across multiple applications? [no]:
Bundle name: MyVendorFOSUserBundle

Target Directory [src/]: src/MyVendor/

Configuration format (annotation, yml, xml, php) [annotation]: 

Bundle generation  

> Generating a sample bundle skeleton into app/../src/MyVendor/MyVendorFOSUserBundle
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/MyVendorFOSUserBundle.php
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/Controller/
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/Controller/DefaultController.php
  created ./app/../tests/MyVendorFOSUserBundle/Controller/
  created ./app/../tests/MyVendorFOSUserBundle/Controller/DefaultControllerTest.php
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/views/Default/
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/views/Default/index.html.twig
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/config/
  created ./app/../src/MyVendor/MyVendorFOSUserBundle/Resources/config/services.yml
> Checking that the bundle is autoloaded
FAILED
> Enabling the bundle inside app/AppKernel.php
  updated ./app/AppKernel.php
OK
> Importing the bundle's routes from the app/config/routing.yml file
  updated ./app/config/routing.yml
OK
> Importing the bundle's services.yml from the app/config/config.yml file
  updated ./app/config/config.yml
OK

  The command was not able to configure everything automatically.  
  You'll need to make the following changes manually.              

- Edit the composer.json file and register the bundle
  namespace in the "autoload" section:

最初的解决方案我尝试修改 composer.json :*

"autoload": {
    "psr-4": {
        "": "src/"  //modif 
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},

有人知道吗

最后,我找到了解决方案:

"autoload": {
    "psr-4": {
        "AppBundle\": "src/AppBundle",
        "MyVendorFOSUserBundle\": "src/MyVendor/MyVendorFOSUserBundle" //Line added
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},

别忘了运行:

composer dumpautoload
  1. 打开 composer.json 文件。

  2. 改变这个:

    "autoload": { "psr-4": { "AppBundle\": "src/AppBundle" },
    

    至:

    "autoload": { "psr-4": { "": "src/" },
    
  3. 运行 这个命令:composer dumpautoload 现在 composer 会生成新的 autoload.php 文件

  4. 运行 命令:php bin/console server:run 一切正常文件。