2017 © Pedro Peláez
 

library mongo

Minimal Mongo Client for Laravel

image

owlgrin/mongo

Minimal Mongo Client for Laravel

  • Wednesday, August 20, 2014
  • by owlgrin
  • Repository
  • 2 Watchers
  • 2 Stars
  • 1,019 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Mongo

Minimal MongoDb for Laravel apps., (*1)

This package provides simple to use MongoDB structure and authentication, to get you started with MongoDB in Laravel in less than 10 seconds., (*2)

Installation

You can install this package by requiring it in your composer.json., (*3)

"owlgrin/mongo": "dev-master"

The add the following service provider in your app.php configuration file., (*4)

'Owlgrin\Mongo\MongoServiceProvider'

Lastly, you will have to list out your MongoDb connection credentials in your database.php configuration file. Add the connections like so:, (*5)

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

To leverage the benefits of replica sets, you can describe the configuration like this:, (*6)

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

Usage

With this package, you can use MongoDb like this:, (*7)

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

If you are using MongoDb exclusively, you can set the default property in database.php configuration file to mongo, to make the usage easier, like so:, (*8)

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

Authentication

This package comes bundled with the authentication driver for MongoDB. To set it up, do these steps., (*9)

  1. Change the driver in app/config/auth.php to 'mongo';
'driver' => 'mongo'
  1. Next, add these two properties (probably next to 'table' property) in the same app/config/auth.php file.
/*
|--------------------------------------------------------------------------
| 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,

That's it! Now, you can authenticate your users using MongoDB. No other change is required in your whole code - it just works!, (*10)


We are not sure, if we will extend the functionalities of this package. We wanted something to get started quickly. If you want something more extensive, Jens has a great package for you: https://github.com/jenssegers/Laravel-MongoDB., (*11)

The Versions

20/08 2014

dev-master

9999999-dev

Minimal Mongo Client for Laravel

  Sources   Download

The Requires

 

by Mohit Mamoria