Class 'MongoClient' 未在文件 Controller\AppController 中找到。php

Class 'MongoClient' not found in file Controller\AppController.php

您好,我尝试使用 CakePHP (version 3.5.1) with Apache server on PHP (7.2.4) 连接 mongo DB(Version 3.4.18) 并遇到以下错误

 2019-07-01 10:41:09 Error: [Error] Class 'MongoClient' not found in E:\xampp\htdocs\myproject\src\Controller\AppController.php on line 142
Request URL: /
Stack Trace:
#0 E:\xampp\htdocs\myproject\src\Controller\PagesController.php(219): App\Controller\AppController->__connectmongodb()
#1 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Controller\Controller.php(441): App\Controller\PagesController->home()
#2 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(119): Cake\Controller\Controller->invokeAction()
#3 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(93): Cake\Http\ActionDispatcher->_invoke(Object(App\Controller\PagesController))
#4 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\BaseApplication.php(103): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#5 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#6 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php(107): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#7 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#8 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php(88): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#9 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#10 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php(93): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#11 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#12 E:\xampp\htdocs\myproject\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php(52): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#13 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): DebugKit\Middleware\DebugKitMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#14 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#15 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Server.php(81): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#16 E:\xampp\htdocs\myproject\webroot\index.php(40): Cake\Http\Server->run()
#17 {main}

对于 mongo 数据库连接尝试下面的代码

public function __connectmongodb()
{

    $m = new \MongoClient('mongodb://localhost:27017');
    return $db = $m->myDb;
}

注意:我必须在 mongo DB(Version 3.4.18) 中使用 CakePHP (version 3.5.1) with Apache server on PHP (7.2.4) 这些特定版本

更新 - 我们尝试 windows 使用 php_mongodb-1.4.0-7.2-ts-vc15-x64 这个 .dll 文件

php_mongodb-1.4.0-7.2-ts-vc15-x64 是 "new" 或更确切地说是唯一官方支持的驱动程序 https://pecl.php.net/package/mongodb. Its usage is documented here https://www.php.net/manual/en/set.mongodb.php 并且基本上使用 MongoDB\Driver\Manager.

MongoClient 来自遗留且很久以前就弃用的驱动程序:https://pecl.php.net/package/mongo. It is documented here https://www.php.net/manual/en/book.mongo.php

PHP7 的旧驱动程序没有官方支持:https://jira.mongodb.org/browse/PHP-1474

上次在 https://docs.mongodb.com/ecosystem/drivers/php/ back in February 2018 上列出遗留驱动程序的时间。到那时它声称与 mongodb 服务器 v2.4、v2.6 和 v3.0 兼容,但与数据库 v3.2+ 不兼容。

同 2018 年 2 月 mongodb v3.0 停用。 Mongodb v3.2 的支持已于 2018 年晚些时候停止,v3.4 将其最后几个月的支持计算到 2020 年 1 月。

长话短说,不,你实际上并不希望使用 MongoClient