Error: The autoloader expected class ... to be defined in file. inSymfony 3.4

Error: The autoloader expected class ... to be defined in file. inSymfony 3.4

为了更新到Symfony 4.0,我在services.yml中添加了“_default”和“AppBundle”并验证了运行,但是出现了如下错误
我有两个bundle,想分别实现自动布线
有什么问题吗?
https://symfony.com/doc/4.0/service_container/3.3-di-changes.html

错误码

The autoloader expected class "App\Sp\AppBundle\AppBundle" to   
  be defined in file "/home/vagrant/Symfony2/vendor/composer/../../src/App/Sp/AppBundle/AppBundle.php". The file was found but the class was not in it, the class name or namespace probably has a typo.  

services.yml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    AppBundle\:
        resource: '../../src/App/Sp/AppBundle/*'
        exclude: '../../src/App/Sp/AppBundle/{Entity,Repository,AppBundle.php}'

    CommonBundle\:
        resource: '../../src/App/Sp/CommonBundle/*'
        exclude: '../../src/App/Sp/CommonBundle/{Entity,Repository}'

AppBundle.php

<?php
namespace App\Sp\AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AhiSpAdminBundle extends Bundle
{
}

我对 Symfony 有点熟悉,但我的经验是开发现有项目而不是创建新项目,但查看错误:... The file was found but the class was not in it, the class name or namespace probably has a typo.

我想这就解释了?文件名为 AppBundle.php,但 class 名称为 AhiSpAdminBundle。 PHP 通常对文件和 class 名称使用相同的名称,所以我认为您应该将 class 名称重命名为 AppBundle 或者只重命名文件名称,基本上确保它们正在使用相同的名称。 (我假设 Symfony 正在根据此“约定”在 AppBundle.php 文件中查找名为 AppBundle 的 class)

作曲家

默认情况下,Symfony Flex 中的 composer.json PSR-4 自动加载器从 /src 路径为 App 应用命名空间。 App 命名空间将应用于 src/ 目录中的所有子目录和文件。

composer.json

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

Symfony 命名约定

Symfony 命名约定还要求 *Bundle.php 文件位于同名 [sic] and [sic] 的命名空间中。这由供应商或类别名称 (Sp) 和名称空间 (AhiSpAdminBundle) 组成,构成 SpAhiSpAdminBundle.

| Namespace Bundle       | Class Name     |
|------------------------|----------------|
| Acme\Bundle\BlogBundle | AcmeBlogBundle |
| Acme\BlogBundle        | AcmeBlogBundle |
// src/Acme/BlogBundle/AcmeBlogBundle.php
namespace App\Acme\BlogBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeBlogBundle extends Bundle
{
}

PSR-4 自动加载

由于命名空间是 App\Sp\AhiSpAdminBundle,因此需要更改文件名和目录以匹配命名空间和 Symfony 包 class 名称约定,分别为 src/Sp/AhiSpAdminBundle/SpAhiSpAdminBundle.php.

重命名文件以匹配 PSR-4 自动加载器的 class 名称。

mv src/Sp/AppBundle/AppBundle.php src/Sp/AppBundle/SpAhiSpAdminBundle.php

重命名目录以匹配 PSR-4 自动加载器的命名空间。

mv src/Sp/AppBundle src/Sp/AhiSpAdminBundle

Symfony 服务

修复您的包的名称空间和 class 名称以遵守 Symfony naming conventions

// src/Sp/AhiSpAdminBundle/SpAhiSpAdminBundle.php
namespace App\Sp\AhiSpAdminBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SpAhiSpAdminBundle extends Bundle
{
  //...
}

然后更新您的服务定义以匹配与 Symfony 兼容的 PSR-4 路径。

# config/services.yaml
services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\Sp\AhiSpAdminBundle\:
        resource: '../src/Sp/AhiSpAdminBundle/*'
        exclude: '../src/Sp/AhiSpAdminBundle/{Entity,Repository,SpAhiSpAdminBundle.php}'

    App\Sp\CommonBundle\:
        resource: '../src/Sp/CommonBundle/*'
        exclude: '../src/Sp/CommonBundle/{Entity,Repository}'

正在清理

生成自动加载器文件。

composer dump-autoload

清除并预热 Syfmony 缓存。

rm -rf var/cache/dev
bin/console --env=dev cache:warmup

如果需要,请为 CommonBundle 命名空间、class 名称和 src/Sp/CommonBundle/SpCommonBundle.php 的文件名重复此过程,或者按照命名约定进行重构以实现所需的最终结果。

// src/Sp/CommonBundle/SpCommonBundle.php
namespace App\Sp\CommonBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class SpCommonBundle extends Bundle
{
  //...
}

While the vendor/category name (Sp) is optional in the bundle class name, with non-descriptive names like AdminBundle and CommonBundle, it is best-practice to include the category in the bundle name, as to ensure there are no naming conflicts and to better differentiate between them when debugging and unit-testing.