Symfony 3.4:生成bundle:ClassNotFoundException:试图加载类" MyVendorFOSUserBundle"来自命名空间" MyVendorFOSUserBundle"

时间:2018-04-29 18:10:15

标签: symfony autoload symfony-3.4

大家好我从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"
    ]
},

有人对此有任何想法

2 个答案:

答案 0 :(得分:1)

  1. 打开composer.json文件。

  2. 更改此:

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

    收件人:

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

  4. 运行命令:php bin /控制台服务器:运行一切正常的文件。

答案 1 :(得分:0)

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

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

别忘了跑:

composer dumpautoload