【发布时间】:2017-08-18 10:52:33
【问题描述】:
好的,所以我有一个项目,我的 mongo 连接在查看页面等时工作正常。但是当我尝试在命令 (ContainerAwareCommand) 中加载管理类时,我得到类未找到异常:
[Symfony\Component\Debug\Exception\ClassNotFoundException]
尝试从命名空间“MongoDB\Driver”加载类“Manager”。
您是否忘记了另一个名称空间的“使用”语句?
我正在使用 symfony 3.2
这是我的工作:
class TestCommand extends ContainerAwareCommand {
protected function configure() {
$this->setName('testcommand:test')
->setDescription('Test...')
->setHelp('Test...');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$db = new Manager("mongodb://localhost:27017");
$output->writeln([
'Test',
'========================',
'',
]);
}
}
我使用以下方式导入驱动程序:
use MongoDB\Driver\Manager;
PhpStorm 识别导入。
任何想法阻止类加载?再次,当我浏览我的页面等时,它会正常加载。
谢谢!
更新(根据要求)!
注册服务:
services:
test.service:
class: AppBundle\Test\TestService
public: true
服务:
namespace AppBundle\Test;
use MongoDB\Driver\Manager;
class TestService {
public function foo() {
$manager = new Manager("mongodb://localhost:27017");
}
}
将此添加到执行方法:
$ts = $this->getContainer()->get("test.service");
$ts->foo();
结果:
[Symfony\Component\Debug\Exception\ClassNotFoundException]
尝试从命名空间“MongoDB\Driver”加载类“Manager”。
您是否忘记了另一个名称空间的“使用”语句?
我错过了什么?
另一个更新
我还注意到我无法切换到生产,因为同样但更普遍的错误。
原来我不能使用命名空间,但可以通过完全限定名在运行时获取类(管理器)!!!
【问题讨论】:
-
从容器中获取管理器。
-
@AlexBlex 我创建了一个服务,该服务加载了 mongo 管理器,从容器中导入它导致了同样的错误。
-
将更新后的代码发布到您将其作为服务/从容器中拉入的位置