【发布时间】:2018-05-15 13:28:21
【问题描述】:
我正在尝试使用 MongoDB 创建一个新的 Symfony4 项目。
首先我使用这个文档创建了一个 Symfony4 项目: https://symfony.com/doc/current/setup.html
然后我使用此文档包含了 MongoDB: http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html
我尽量按照说明进行操作(例如,我不需要向 app/AppKernel.php 添加任何内容,但 MongoDB 会自动添加到 config/bundles.php)。
现在我认为一切正常,但我的 Symfony 应用程序找不到 MongoDB 服务:
You have requested a non-existent service "doctrine_mongodb".
Did you mean one of these: "http_kernel", "request_stack", "router"?
in ServiceLocator.php (line 48)
控制器:
namespace App\Controller;
use App\Document\Chapter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends AbstractController {
public function createAction() {
$test = new Chapter();
$test->setHeadline('Test');
$dm = $this->get('doctrine_mongodb')->getManager();
$dm->persist($test);
$dm->flush();
return new Response('Created product id '.$test->getId());
}
}
但是,如果我在控制台上执行此操作:
php bin/console debug:container
我得到一个服务列表,其中包括:
doctrine_mongodb Doctrine\Bundle\MongoDBBundle\ManagerRegistry
doctrine_mongodb.odm.default_connection Doctrine\MongoDB\Connection
doctrine_mongodb.odm.default_document_manager Doctrine\ODM\MongoDB\DocumentManager
doctrine_mongodb.odm.document_manager alias for "doctrine_mongodb.odm.default_document_manager"
所以服务似乎在那里,但 Symfony 无法从我的应用程序中加载它。
知道如何解决这个问题吗? 是否有可能 Mongo-DB 服务器连接不起作用并且由于某种原因它没有被记录并且服务不会加载?
【问题讨论】:
-
页面symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.htm 说“您要查找的页面不存在。您可以改为浏览以下任何资源。”
-
如果我不得不猜测,我可能会怀疑 mongodb 服务是私有的,需要注入。您的问题中没有足够的信息。您发布的代码是控制器的一部分吗?你是从 AbstractController 扩展而来的吗? AbstractController 限制对容器的访问。
-
@AlexBlex 这是正确的链接(我编辑了我的帖子):symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.ht
-
@Cerad:我编辑了我的帖子以包含来自此示例的完整控制器:symfony.com/doc/current/bundles/DoctrineMongoDBBundle/…
-
唯一有效的链接在最后一条评论中,它指向 Symfony 3 的文档。