owlgrin/mongo

此包的最新版本(dev-master)没有可用的许可证信息。

Laravel 最小化 MongoDB 客户端

dev-master 2014-08-20 13:17 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:15:23 UTC


README

为 Laravel 应用提供最小化 MongoDB。

此包提供简单易用的 MongoDB 结构和身份验证,让您在不到 10 秒内开始使用 MongoDB。

安装

您可以通过在 composer.json 中添加此包来安装它。

"owlgrin/mongo": "dev-master"

然后在您的 app.php 配置文件中添加以下服务提供者。

'Owlgrin\Mongo\MongoServiceProvider'

最后,您需要在 database.php 配置文件中列出您的 MongoDB 连接凭证。添加连接方式如下

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

要利用副本集的好处,您可以这样描述配置

...
'connections' => array(
	...
	'mongo' => array(
		'driver'   => 'mongo',
		'host'     => array('server1', 'server2'),
		'port'     => 27017,
		'username' => 'username',
		'password' => 'password',
		'database' => 'experiment',
		'options'  => array('replicaSet' => 'someReplicaSet')
	)
)

使用方法

有了这个包,您可以使用 MongoDb 如下

$user = DB::connection('mongo')
	->users
	->find(array('is_active' => 1));

如果您只使用 MongoDb,您可以将 database.php 配置文件中的 default 属性设置为 mongo,以简化使用,如下所示

$users = DB::collection('users')->find(array('is_active' => 1));

身份验证

此包捆绑了 MongoDB 的身份验证驱动程序。要设置它,请执行以下步骤。

  1. app/config/auth.php 中将驱动程序更改为 'mongo';
'driver' => 'mongo'
  1. 接下来,在相同的 app/config/auth.php 文件中添加这两个属性(可能紧挨着 'table' 属性)。
/*
|--------------------------------------------------------------------------
| Authentication Collection
|--------------------------------------------------------------------------
|
| When using the "Mongo" authentication driver, we need to know which
| collection should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/

'collection' => 'users',

/*
|--------------------------------------------------------------------------
| Hashed or Encrypted Password
|--------------------------------------------------------------------------
|
| In some cases, you may need to store passwords encrypted as opposed to
| hashed generally. For instance, API secret keys for your users can be stored
| encrypted and not hashed, as they are to be decrypted and shown to
| users in their accounts (and storing them in plain text is a very bad idea).
| By default, we assume that the password will be hashed. If it is encrypted,
| set the following option to true.
|
*/

'encrypted_password' => false,

就这样!现在,您可以使用 MongoDB 对用户进行身份验证。您的整个代码不需要任何其他更改 - 它只需工作即可!

我们不确定是否将扩展此包的功能。我们希望快速开始。如果您需要更广泛的功能,Jens 有一个很好的包:[https://github.com/jenssegers/Laravel-MongoDB](https://github.com/jenssegers/Laravel-MongoDB)。