创建一个新项目和包 - "Did you forget a 'use' statement for another namespace?"
Creating a new project and bundle - "Did you forget a 'use' statement for another namespace?"
我正在尝试使用测试包创建一个测试项目。我收到以下错误:
Attempted to load class "TestBundle" from namespace "test". Did you forget a "use" statement for another namespace?
我已经阅读了 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 安装“捆绑包”,因此不会生成自动加载器,并且永远不会找到与您的新包相关的 class。
包的 composer.json
文件无关紧要,因为您不是通过 composer 安装包。因此,它从未被阅读过。
但是您可以指示 composer 生成将这个新包考虑在内的自动加载器文件。
假设这是您的目录结构:
- config <--- where bundles.php resides, among other files
- src <--- the application code that "consumes" your bundle,
- testBundle <--- This is where you have your bundle's code.
- vendor
- etc
请注意,您的捆绑代码 与应用程序代码的其余部分不在同一目录中 。
现在,在您的 应用程序 composer.json
文件中,您应该需要添加如下内容:
{
"autoload": {
"psr-4":
{
"App\": "src/",
"test\TestBundle\": "testBundle/"
}
}
}
完成此操作后,您应该重新转储自动加载器 (composer dump-autoload
),文件应该可以被发现。
(请注意,在您的问题中,您说 TestBundle
的命名空间是 test\TestBundle
,但随后在 bundles.php
中您尝试使用 test\TestBundle::class
。其中之一这些东西是错误的,它要么是 namespace test;
,要么是 test\TestBundle\TestBundle::class
)。
我正在尝试使用测试包创建一个测试项目。我收到以下错误:
Attempted to load class "TestBundle" from namespace "test". Did you forget a "use" statement for another namespace?
我已经阅读了 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 安装“捆绑包”,因此不会生成自动加载器,并且永远不会找到与您的新包相关的 class。
包的 composer.json
文件无关紧要,因为您不是通过 composer 安装包。因此,它从未被阅读过。
但是您可以指示 composer 生成将这个新包考虑在内的自动加载器文件。
假设这是您的目录结构:
- config <--- where bundles.php resides, among other files
- src <--- the application code that "consumes" your bundle,
- testBundle <--- This is where you have your bundle's code.
- vendor
- etc
请注意,您的捆绑代码 与应用程序代码的其余部分不在同一目录中 。
现在,在您的 应用程序 composer.json
文件中,您应该需要添加如下内容:
{
"autoload": {
"psr-4":
{
"App\": "src/",
"test\TestBundle\": "testBundle/"
}
}
}
完成此操作后,您应该重新转储自动加载器 (composer dump-autoload
),文件应该可以被发现。
(请注意,在您的问题中,您说 TestBundle
的命名空间是 test\TestBundle
,但随后在 bundles.php
中您尝试使用 test\TestBundle::class
。其中之一这些东西是错误的,它要么是 namespace test;
,要么是 test\TestBundle\TestBundle::class
)。