gpt4 book ai didi

php - 如何在测试网络应用程序中使用 MongoDB PHP 库

转载 作者:可可西里 更新时间:2023-11-01 10:48:52 27 4
gpt4 key购买 nike

我安装了一个新的服务器,运行(alpine)linux 和 Apache 和 PHP7。我还安装了 mongodb 驱动程序。以下是我在运行 phpinfo() 时看到的内容:

mongodb support enabled
mongodb version 1.1.10-dev
mongodb stability devel
libmongoc version 1.3.6
libbson version 1.3.6

我还安装了 mongodb php7 库 ( https://pkgs.alpinelinux.org/package/edge/testing/x86_64/mongo-php7-library ),它为我创建了以下目录结构:

webdev1:/var/www/# ls -lah /usr/lib/php7/vendor/mongodb/mongodb/
total 104
drwxr-xr-x 6 root root 4.0K Aug 16 18:03 .
drwxr-xr-x 3 root root 4.0K Aug 15 18:07 ..
-rw-r--r-- 1 root root 5.1K Sep 23 2016 BulkWriteResult.php
-rw-r--r-- 1 root root 5.3K Aug 16 18:03 Client.php
-rw-r--r-- 1 root root 27.1K Sep 23 2016 Collection.php
-rw-r--r-- 1 root root 9.6K Sep 23 2016 Database.php
-rw-r--r-- 1 root root 1.3K Sep 23 2016 DeleteResult.php
drwxr-sr-x 2 root root 4.0K Aug 15 18:07 Exception
-rw-r--r-- 1 root root 2.0K Sep 23 2016 InsertManyResult.php
-rw-r--r-- 1 root root 2.0K Sep 23 2016 InsertOneResult.php
drwxr-sr-x 2 root root 4.0K Aug 15 18:07 Model
drwxr-sr-x 2 root root 4.0K Aug 15 18:07 Operation
-rw-r--r-- 1 root root 3.3K Sep 23 2016 UpdateResult.php
-rw-r--r-- 1 root root 4.3K Sep 23 2016 functions.php
drwxr-sr-x 6 root root 4.0K Aug 16 17:41 tests

我现在正在尝试编写一个基本页面,以证明该库正在运行。
到目前为止,我有:

  1. 将库文件复制到我的测试项目的子文件夹中。所以我在我的测试网络应用程序文件夹中有这个:

    webdev1:/var/www/localhost/htdocs/test# ls -lah
    total 16
    drwxr-sr-x 3 root root 4.0K Aug 16 18:41 .
    drwxr-sr-x 4 root root 4.0K Aug 16 18:30 ..
    drwxr-xr-x 6 root root 4.0K Aug 16 18:44 mongodb
    -rw-r--r-- 1 root root 516 Aug 16 18:39 test.php

    webdev1:/var/www/localhost/htdocs/test# ls -lah mongodb/
    total 104
    drwxr-xr-x 6 root root 4.0K Aug 16 18:44 .
    drwxr-sr-x 3 root root 4.0K Aug 16 18:41 ..
    -rw-r--r-- 1 root root 5.1K Aug 16 18:32 BulkWriteResult.php
    -rw-r--r-- 1 root root 5.3K Aug 16 18:32 Client.php
    -rw-r--r-- 1 root root 27.1K Aug 16 18:32 Collection.php
    -rw-r--r-- 1 root root 9.6K Aug 16 18:32 Database.php
    -rw-r--r-- 1 root root 1.3K Aug 16 18:32 DeleteResult.php
    drwxr-sr-x 2 root root 4.0K Aug 16 18:32 Exception
    -rw-r--r-- 1 root root 2.0K Aug 16 18:32 InsertManyResult.php
    -rw-r--r-- 1 root root 2.0K Aug 16 18:32 InsertOneResult.php
    drwxr-sr-x 2 root root 4.0K Aug 16 18:32 Model
    drwxr-sr-x 2 root root 4.0K Aug 16 18:32 Operation
    -rw-r--r-- 1 root root 3.3K Aug 16 18:32 UpdateResult.php
    -rw-r--r-- 1 root root 4.3K Aug 16 18:32 functions.php
    drwxr-sr-x 6 root root 4.0K Aug 16 18:41 tests
  2. 使用以下代码创建一个 test.php 页面:

    "; echo extension_loaded("mongodb") ? "加载\n": "未加载\n";echo "测试 2 - MongoDB 客户端库工作...
    ";$client = new MongoDB\Client("mongodb://localhost:27017");$collection = $client->jjtest->col1;$result = $collection->find();变量转储($结果);?>

结果

当我运行 test.php 页面时,它显示我的驱动程序已加载...但我收到错误:

Test 1 - Mongodb Driver loaded?
loaded Test 2 - MongoDB Client Library working...

Fatal error: Uncaught Error: Class 'MongoDB\Database' not found in /var/www/localhost/htdocs/test/mongodb/Client.php:168 Stack trace: #0 /var/www/localhost/htdocs/test/mongodb/Client.php(93): MongoDB\Client->selectDatabase('jjtest') #1 /var/www/localhost/htdocs/test/test.php(9): MongoDB\Client->__get('jjtest') #2 {main} thrown in /var/www/localhost/htdocs/test/mongodb/Client.php on line 168

到目前为止我尝试了什么

为了证明 mongo 本身在工作,我启动了一个 CLI 并运行了这个命令:

> use jjtest
switched to db jjtest
> show collections
col1
> db.col1.find();
{ "_id" : ObjectId("5994980ba9e72bb4f6afed66"), "name" : "john", "age" : "99" }
>

我目前正在阅读有关 namespace 和 autoloader.php ( Why my autoload.php of composer doesn't work? ) 的资料,但到目前为止我尝试过的任何内容都无法在那里工作。通过安装 mongo-php7-library 自动创建的文件不包含我可以指向的 autoloader.php。

更新 1

我还尝试将其包含在我的 test.php 文件的顶部:

 require_once __DIR__ . "/mongodb/";

但是我得到以下错误:

Warning: require_once(/var/www/localhost/htdocs/test/mongodb/): failed to open stream: No error information in /var/www/localhost/htdocs/test/test.php on line 1

Fatal error: require_once(): Failed opening required '/var/www/localhost/htdocs/test/mongodb/' (include_path='.:') in /var/www/localhost/htdocs/test/test.php on line 1

最佳答案

您正在为 php 的类加载而苦苦挣扎。目前,您正在使用 mongodb/Client.php 的单个 require 语句自己执行此操作,但是这个文件引用了其他未加载的类(因此:文件)。这就是 php 提示缺少类的原因。正如您已经注意到的那样,对文件夹调用 require 没有帮助。

除非您想坚持对项目中的每个类都进行 require 调用,否则您现在应该检查自动加载。在 php 中,自动加载基本上是一个函数,它在运行时为代码引用的每个类触发并加载包含类定义的正确文件。

您可以将一个简单示例包含到您的 test.php 中:

<?php

/**
* Load file with class
*
* @param string $class
*
*/
function mongo_autoload($class)
{
/**
* All classes of your library are defined within the "MongoDB" namespace.
* Example: MongoDB\Model\BSONArray
* Each part after "MongoDB" refers to a folder, the final part is the filename (without .php suffix).
* We strip the MongoDBm add .php suffix and load the file from mongodb/ folder
*/
$filename = str_replace('MongoDB\\', '', $class) . '.php';
require 'mongodb/' . $filename;
}

/**
* Here we register our function as autoload method
*/
spl_autoload_register('mongo_autoload');


//proof that the mongo driver is loaded:
echo "Test 1 - Mongodb Driver loaded?<BR>";
echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n";

echo "Test 2 - MongoDB Client Library working...<BR>";
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->jjtest->col1;
$result = $collection->find();
var_dump($result);
?>

以这种方式工作不是很方便,因为 php 类和命名空间有不同的命名策略,如果你有很多依赖的包,你无法控制使用哪一个。在 php-land 我们得到了 composer为此,它管理您的项目所依赖的每个包,并为它们创建适当的自动加载策略。安装依赖项后,您只需要包含一个 autload.php。您的库也随 Composer 一起分发,请参阅他们的自述文件;)

关于php - 如何在测试网络应用程序中使用 MongoDB PHP 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45721261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com