laravel 找不到类“MongoDB\Driver\Manager”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35920568/
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
Class 'MongoDB\Driver\Manager' not found
提问by garden
I'm integrating MongoDB with Laravel. I'm following information found in this link jenssegers/laravel-mongodb. I'm successful at integrating MongoDB. But when I run the code it throws this error
我正在将 MongoDB 与 Laravel 集成。我正在关注此链接jenssegers/laravel-mongodb 中的信息。我成功地集成了 MongoDB。但是当我运行代码时它会抛出这个错误
FatalErrorException in Client.php line 56: Class 'MongoDB\Driver\Manager' not found
Client.php 第 56 行中的 FatalErrorException: Class 'MongoDB\Driver\Manager' not found
Here is the code
这是代码
Controllerapp/Http/Controller/NewController
控制器应用程序/Http/Controller/NewController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class NewController extends Controller
{
//
public function index(){
$var = User::all();
var_dump($var);
}
}
And here follows the user.php
that is by default provided by Laravel when we create the project
下面user.php
是我们创建项目时 Laravel 默认提供的
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class User extends Eloquent
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $collection = 'users_collection';
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Please tell me why am I getting this error.
请告诉我为什么会出现此错误。
Phpinfo --> mongodb section is coming. Here is a screen shot phpinfo
Phpinfo --> mongodb 部分来了。这是一个屏幕截图 phpinfo
回答by J.C. Gras
I do not know if it's the most elegant solution, but it worked for me:
我不知道这是否是最优雅的解决方案,但它对我有用:
- Install the php driver
$ sudo pecl install mongodb
- Create the extension file
$ sudo nano /etc/php5/mods-available/mongodb.ini
and write inside: extension=mongodb.so - Create a symbolic link for this file
$ sudo ln -sv /etc/php5/mods-available/mongodb.ini /etc/php5/apache2/conf.d/20-mongodb.ini
- Create an other symbolic link for this file
$ sudo ln -sv /etc/php5/mods-available/mongodb.ini /etc/php5/cli/conf.d/20-mongodb.ini
- Restart apache or the server used
$ sudo service apache2 restart
- 安装php驱动
$ sudo pecl install mongodb
- 创建扩展文件
$ sudo nano /etc/php5/mods-available/mongodb.ini
并在里面写:extension=mongodb.so - 为这个文件创建一个符号链接
$ sudo ln -sv /etc/php5/mods-available/mongodb.ini /etc/php5/apache2/conf.d/20-mongodb.ini
- 为此文件创建另一个符号链接
$ sudo ln -sv /etc/php5/mods-available/mongodb.ini /etc/php5/cli/conf.d/20-mongodb.ini
- 重启apache或使用的服务器
$ sudo service apache2 restart
It may be necessary to reinstall jenssegers/mongodb: $ composer require jenssegers/mongodb
可能需要重新安装 jenssegers/mongodb: $ composer require jenssegers/mongodb
回答by Albert s
You may also need to restart fpm on ubuntu/debian
您可能还需要在 ubuntu/debian 上重新启动 fpm
sudo service php7.0-fpm restart
for php5
用于 php5
sudo service php5-fpm restart
Also, make sure extension=mongodb.so
is in:
另外,请确保extension=mongodb.so
在:
apache2/php.ini
cli/php.ini
fpm/php.ini
to check i do:
检查我做:
locate php.ini | xargs grep mongo
to see php.ini files:
查看 php.ini 文件:
php --ini
回答by Rav
As of 2017 this should work for PHP 5.6:
截至 2017 年,这应该适用于 PHP 5.6:
### Install MongoDB driver for PHP 5.6 ##############
echo -e "\n### Installing MongoDB PHP driver ##\n";
sudo apt-get install php-mongodb -y
# Make sure extension=mongodb.so is present
PhpConfigFile="/etc/php/5.6/fpm/php.ini"
if grep -q mongodb.so "$PhpConfigFile"; then
echo "extension=mongodb.so already found, dont add."
else
echo "extension=mongodb.so" | sudo tee "$PhpConfigFile"
echo "Added extension=mongodb.so to $PhpConfigFile"
fi
It is part of my one-liner installer of PHP5.6 + nginx + MongoDB plugin for PHP, I've even made a domain for it: http://ubuntu-script.tk
它是我的 PHP5.6 + nginx + MongoDB 插件单行安装程序的一部分,我什至为它创建了一个域:http: //ubuntu-script.tk
回答by joy jedidja Ndjama
Maybe it could help you.
也许它可以帮助你。
If you are using the new Wamp server, make sure that the php interpreter that your ide is using if the same use by wamp and where you have install the mongodb.dll.
如果您使用的是新的 Wamp 服务器,请确保您的 ide 使用的 php 解释器与 wamp 使用的相同并且您安装了 mongodb.dll。
I was having the same issue.
我遇到了同样的问题。
I resolved it when i installed mongodb.dll both on php 5.6.25 and 7.0.10, because my wamp server have the two versions. After the installation, i made a composer update. Now it is ok.
我在 php 5.6.25 和 7.0.10 上安装 mongodb.dll 时解决了它,因为我的 wamp 服务器有两个版本。安装后,我进行了作曲家更新。现在好了。
回答by mcmacerson
The answer can be found here: Why MongoDB Class doesn't work in Laravel?
答案可以在这里找到: 为什么 MongoDB 类在 Laravel 中不起作用?
apokryfosgives us the answer in the first comment:
apokryfos在第一条评论中给了我们答案:
Try new \MongoClient() instead
尝试使用 new \MongoClient() 代替
For anyone struggling to get the mongodb.soextension working for PHP, be careful, there are two mongo drivers for PHP. For the new driver follow these instructions: http://php.net/manual/en/mongodb.installation.manual.php
对于任何努力使mongodb.so扩展适用于 PHP 的人,请注意,有两个适用于 PHP 的 mongo 驱动程序。对于新驱动程序,请遵循以下说明:http: //php.net/manual/en/mongodb.installation.manual.php
Make sure all your php.ini files contain:
确保所有 php.ini 文件都包含:
extension=mongodb.so
From the root of your project:
从项目的根目录:
composer require mongodb/mongodb
In your PHP file:
在您的 PHP 文件中:
NOTE THE BACKSLASH BEFORE MongoDB
注意 MongoDB 之前的反斜杠
$client = new \MongoDB\Client("mongodb://127.0.0.1:27017");
$collection = $client->database->collection;
$cursor = $collection->find(
[
'some.nice.name' => 'true'
]
);
回答by Dev Semicolon
When you are going to connect MongoDB database with any PHP application then you can face this issue if you don't have PHP MongoDB driver.
当您要将 MongoDB 数据库与任何 PHP 应用程序连接时,如果您没有 PHP MongoDB 驱动程序,则可能会遇到此问题。
So first you need to run following command to install Mongodb PHP extension :
所以首先你需要运行以下命令来安装 Mongodb PHP 扩展:
sudo apt-get install php-mongodb
Now restart the server :
现在重新启动服务器:
sudo service apache2 restart
回答by Ligemer
I'm going to assume that you have php-mongodb installed correctly no matter what the process.
无论过程如何,我都假设您已正确安装了 php-mongodb。
I hit this error when the mongo connection string was empty:
当 mongo 连接字符串为空时,我遇到了这个错误:
new MongoDB\Client('mongodb://');
Make sure your mongo connection string is populated properly.
确保您的 mongo 连接字符串已正确填充。