错误:自动加载器期望的类.在文件中定义.inSymfony 3.4



对于Symfony 4.0的更新,我添加了"_default"one_answers";AppBundle"服务。Yml并验证了操作,但是发生了以下错误。
我有两个bundle,想为每个bundle实现自动连接。
有什么问题吗?https://symfony.com/doc/4.0/service_container/3.3-di-changes.html

错误代码

The autoloader expected class "AppSpAppBundleAppBundle" 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 AppSpAppBundle;
use SymfonyComponentHttpKernelBundleBundle;
class AhiSpAdminBundle extends Bundle
{
}

作曲家

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

composer.json

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

Symfony命名约定

Symfony命名约定还要求*Bundle.php文件驻留在具有相同名称[sic]和[sic]的命名空间中。它由供应商或类别名称(Sp)和命名空间(AhiSpAdminBundle)组成,用于生成SpAhiSpAdminBundle

| Namespace Bundle       | Class Name     |
|------------------------|----------------|
| AcmeBundleBlogBundle | AcmeBlogBundle |
| AcmeBlogBundle        | AcmeBlogBundle |
// src/Acme/BlogBundle/AcmeBlogBundle.php
namespace AppAcmeBlogBundle;
use SymfonyComponentHttpKernelBundleBundle;
class AcmeBlogBundle extends Bundle
{
}

PSR-4半自动的

由于命名空间是AppSpAhiSpAdminBundle,因此文件名和目录需要更改,以使命名空间和Symfony bundle类的名称约定分别与src/Sp/AhiSpAdminBundle/SpAhiSpAdminBundle.php相匹配。

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

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

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

mv src/Sp/AppBundle src/Sp/AhiSpAdminBundle

Symfony服务修改bundle的命名空间和类名,使其符合Symfony的命名约定。

// src/Sp/AhiSpAdminBundle/SpAhiSpAdminBundle.php
namespace AppSpAhiSpAdminBundle;
use SymfonyComponentHttpKernelBundleBundle;
class SpAhiSpAdminBundle extends Bundle
{
//...
}

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

# config/services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
AppSpAhiSpAdminBundle:
resource: '../src/Sp/AhiSpAdminBundle/*'
exclude: '../src/Sp/AhiSpAdminBundle/{Entity,Repository,SpAhiSpAdminBundle.php}'
AppSpCommonBundle:
resource: '../src/Sp/CommonBundle/*'
exclude: '../src/Sp/CommonBundle/{Entity,Repository}'

清理生成自动加载器文件。

composer dump-autoload

清空并预热syfony缓存

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

如果需要,对CommonBundle名称空间、src/Sp/CommonBundle/SpCommonBundle.php的类名和文件名重复该过程,或者按照命名约定进行重组,以获得期望的最终结果。

// src/Sp/CommonBundle/SpCommonBundle.php
namespace AppSpCommonBundle;
use SymfonyComponentHttpKernelBundleBundle;
class SpCommonBundle extends Bundle
{
//...
}

而供应商/类别名称(Sp)在bundle类中是可选的名称,具有非描述性名称,如AdminBundle和在CommonBundle中,最好的做法是将类别包含在包的名称,以确保没有命名冲突和更好在调试和单元测试时要区分它们。

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

我想这就解释了吧?文件名为AppBundle.php,类名为AhiSpAdminBundle。PHP通常使用相同的文件名作为文件和类名所以我认为你应该将类名重命名为AppBundle或者只是重命名文件名,确保它们使用相同的名称。(我假设Symfony正在寻找一个类命名为AppBundleAppBundle.php文件根据这个"约定")

最新更新