Laravel 4: Class 'MongoClient' Not Found

2020-07-18 05:56发布

问题:

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

I have successfully setup second virtual host and would like to use a MongoDB backend. After searching around I found out that Laravel does not natively connect to MongoDB and I found https://github.com/jenssegers/Laravel-MongoDB and 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.

I edited composer.json per the instructions:

............
"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 update again, 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 update it consistently throws the same error:

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:

; added for mongoDB connections
extension=php_mongo.dll

But this did not help.

回答1:

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

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/36 Then here: https://github.com/leroy-merlin-br/mongolid-laravel#troubleshooting
The following command and output indicates the location of php.ini that I should have updated with the php_mongo.dll extension:

$ 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 WAMP tray 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:

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

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



回答2:

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

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.

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

'default' => 'mongodb',

And add mongodb to your connections too:

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