【发布时间】:2021-09-05 01:12:44
【问题描述】:
我正在尝试使用测试包创建一个测试项目。我收到以下错误:
尝试从命名空间“test”加载类“TestBundle”。您是否忘记了另一个命名空间的“使用”语句?
我已阅读 Symfony 网站上的所有说明并尝试了许多不同的方法,但没有任何乐趣。
test/config/bundles.php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
test\TestBundle::class => ['all' => true],
];
test/src/TestBundle/TestBundle.php
namespace test\TestBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class TestBundle extends Bundle
{
public function getPath(): string
{
return \dirname(__DIR__);
}
}
test/src/TestBundle/composer.json
{
"type": "symfony-bundle",
"name": "TestBundle",
"type": "testing building a reusuable bundle",
"license": "proprietary",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=7.2.5",
"ext-ctype": "*",
"ext-iconv": "*",
"symfony/console": "5.4.*",
"symfony/dotenv": "5.4.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.4.*",
"symfony/runtime": "5.4.*",
"symfony/yaml": "5.4.*"
},
"require-dev": {
},
"config": {
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"test\\TestBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"test\\TestBundle\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*"
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "5.4.*"
}
}
}
test/composer.json
{
"type": "project",
"license": "proprietary",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=7.2.5",
"ext-ctype": "*",
"ext-iconv": "*",
"symfony/console": "5.4.*",
"symfony/dotenv": "5.4.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.4.*",
"symfony/runtime": "5.4.*",
"symfony/yaml": "5.4.*",
"symfony/yaml": "5.4.*",
"test/TestBundle": "*"
},
"require-dev": {
},
"config": {
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "5.4.*"
}
}
}
【问题讨论】:
-
不,我还没到那一步,没有依赖关系,它现在基本上是一个空包,所以它应该不需要composer来安装它吗?
-
按照我认为 bundles.php 的说明操作?
-
不,这无关。
bundles.php怎么能告诉自动加载器任何事情,而文件不知道所引用的类在哪里定义。 -
啊,它是 composer.json 文件吗?我忘记在帖子中添加了,我现在就添加它。
-
请分享更多细节。据我所知,您没有任何名为
test\TestBundle的类,而是一个名为test\TestBundle\TestBundle的类 - 您是否尝试在bundles.php中使用它?
标签: php symfony composer-php