我已经安装了使用Apache和PHP7运行(高山)Linux的新服务器。我还安装了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
我现在正在尝试编写一个基本页面,以证明该库正在运行。
到目前为止,我有:
将库文件复制到测试项目中的子文件夹中。所以我在我的测试Web应用程序文件夹中有这个:
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
使用以下代码创建一个test.php页面:
“;回声extension_loaded(” mongodb“)?” loaded \ n“:”未加载\ n“;回声”测试2-MongoDB客户端库正在运行...结果
当我运行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" }
>
我目前正在阅读名称空间和autoloader.php(为什么我的作曲家的autoload.php无法正常工作?),但是到目前为止,我没有尝试过的任何方法都无法正常工作。通过安装mongo-php7-library自动创建的文件不包含我可以指向的autoloader.php。
更新1
我还尝试将其包含在test.php文件的顶部:
require_once __DIR__ . "/mongodb/";
但是我收到以下错误:
警告:require_once(/ var / www / localhost / htdocs / test / mongodb /):无法打开流:第1行的/var/www/localhost/htdocs/test/test.php中没有错误信息
致命错误:require_once():无法在线上/var/www/localhost/htdocs/test/test.php中打开所需的'/ var / www / localhost / htdocs / test / mongodb /'(include_path ='.:') 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中,我们为此提供了作曲器,它可以管理您的项目所依赖的每个软件包,并为其创建适当的自动加载策略。安装依赖项后,只需包含一个autload.php。您的库也随作曲家一起分发,请参阅他们的自述文件;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句