Laravel 4:找不到类“MongoClient”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24251810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:39:37  来源:igfitidea点击:

Laravel 4: Class 'MongoClient' Not Found

phpmongodblaravel

提问by PeterKA

I have Laravel 4 installed on WAMP and it works great with a MySQL backend.

我在 WAMP 上安装了 Laravel 4,它与 MySQL 后端配合得很好。

I have successfully setup second virtual host and would like to use a MongoDBbackend. After searching around I found out that Laravel does not natively connect to MongoDB and I found https://github.com/jenssegers/Laravel-MongoDBand I have been trying to set it up but I can't seem to get it right. Obviously I must be doing something wrong and I am hoping someone can help me identify what it is that I am not doing right.

我已成功设置第二个虚拟主机并想使用MongoDB后端。四处搜索后,我发现 Laravel 本身并没有连接到 MongoDB,我找到了https://github.com/jenssegers/Laravel-MongoDB,我一直在尝试设置它,但似乎无法正确设置。显然我一定是做错了什么,我希望有人能帮助我确定我做错了什么。

I edited composer.jsonper the instructions:

composer.json按照说明进行了编辑:

............
"license": "MIT",
"require": {
    "laravel/framework": "4.1.*",
    "jenssegers/mongodb": "*"
},
"autoload": {
.........

Then I ran composer update. It installed monolog 1.9.1 and swiftmailer v5.2.0 - whatever these are - successfully (a few days ago) but then threw an error after that. Today I tried to run composer updateagain, and it updated the two to 1.10.0 and v5.2.1 respectively and then encountered the same error. Now when I try composer updateit consistently throws the same error:

然后我跑了composer update。它安装了 monolog 1.9.1 和 swiftmailer v5.2.0 - 不管这些是什么 - 成功(几天前)但之后又抛出了一个错误。今天composer update再次尝试运行,将两者分别更新为1.10.0和v5.2.1,然后又遇到同样的错误。现在,当我尝试composer update它时,它始终会抛出相同的错误:

Nothing to install or update
Generating autoload files
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","me
ssage":"Class 'MongoClient' not found","file":"C:\wamp\www\laravel\vendor\j
enssegers\mongodb\src\Jenssegers\Mongodb\Connection.php","line":132}}Script
 php artisan clear-compiled handling the post-update-cmd event returned with an
error  


  [RuntimeException]
  Error Output:    


update [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--lock]
 [--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [--with-
dependencies] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [packages1] ... [
packagesN]

What I have tried:

我尝试过的

I have downloaded and installed the php_mongo.dll by placing it in the php ext folder and enabling it in php.ini:

我已经下载并安装了 php_mongo.dll,方法是将它放在 php ext 文件夹中并在 php.ini 中启用它:

; added for mongoDB connections
extension=php_mongo.dll

But this did not help.

但这没有帮助。

回答by PeterKA

NOTE:I would not have been able to resolve the issue completely without @Hassan 's help -- please see comments under @Hassan 's answer.

注意:如果没有 @Hassan 的帮助,我将无法完全解决这个问题——请参阅 @Hassan 回答下的评论。

I'll provide this answer in the hopes that it may help someone else who may experience the same issue. I thought it better to give it as an answer so that it stands out.

我将提供此答案,希望它可以帮助可能遇到相同问题的其他人。我认为最好将其作为答案给出,以使其脱颖而出。

Further search lead me here: https://github.com/jenssegers/Laravel-MongoDB/issues/36Then here: https://github.com/leroy-merlin-br/mongolid-laravel#troubleshooting
The following command and output indicates the location of php.inithat I should have updated with the php_mongo.dllextension:

进一步搜索将我带到这里:https: //github.com/jenssegers/Laravel-MongoDB/issues/36然后在这里:https: //github.com/leroy-merlin-br/mongolid-laravel#troubleshooting
以下命令和输出表示php.ini我应该用php_mongo.dll扩展名更新的位置:

$ php -i | grep 'Configuration File'
Configuration File (php.ini) Path => C:\Windows
Loaded Configuration File => C:\wamp\bin\php\php5.5.12\php.ini

The WAMPtray icon however points to C:\wamp\bin\apache2.4.9\bin\php.ini-- which is what I had updated. I also checked if PHP in the CLI environment is importing the driver properly by running the following command with the output shown:

WAMP然而,托盘图标指向C:\wamp\bin\apache2.4.9\bin\php.ini- 这是我更新的内容。我还通过运行以下显示输出的命令来检查 CLI 环境中的 PHP 是否正确导入驱动程序:

$ php -i | grep 'Mongo'
MongoDB Support => enabled

After updating the correct php.ini, I restarted apacheand tried again composer updateagain. The error was different -- authentication -- as the username, password and database were wrong. Once those were corrected, the update completed without incident.

更新正确后php.ini,我restarted apache又试了composer update一次。错误是不同的——身份验证——因为用户名、密码和数据库是错误的。一旦这些问题得到纠正,更新就顺利完成了。

回答by Hassan

Your composer file looks fine, as is probably everything else. Try a "composer dumpautoload", from the docs:

您的 Composer 文件看起来不错,其他一切都一样。尝试使用文档中的“ composer dumpautoload”:

If you need to update the autoloader because of new classes in a classmap package for example, you can use "dump-autoload" to do that without having to go through an install or update.

例如,如果由于类映射包中的新类而需要更新自动加载器,则可以使用“dump-autoload”来执行此操作,而无需进行安装或更新。

This should fix that error, after which you'll need to change your adapter in app/config/database.phpto use mongodblike so:

这应该可以修复该错误,之后您需要将适配器更改app/config/database.php为如下使用mongodb

'default' => 'mongodb',

And add mongodb to your connections too:

并将 mongodb 添加到您的连接中:

'connections' => array(
...
    'mongodb' => array(
        'driver'   => 'mongodb',
        'host'     => 'localhost',
        'port'     => 27017,
        'username' => '',
        'password' => '',
        'database' => 'test'
    ),
),