Lumen and MongoDB? Lumen and MongoDB? laravel laravel

Lumen and MongoDB?


We're actually using Lumen, Laravel, Mongo, and MySQL in one giant project so I can help you through this one. Assuming you want to use MongoDB with eloquent instead of with the raw MongoClient. You can find the library I'm using from jenssegers here.

Install MongoDB Extension

Firstly you'll need to install the dependencies for PHP to interact with mongo. The specifics for installing the mongo extension can be found on the PHP documentation.

After that you'll have to edit the php.ini files for the platforms (apache/cli/nginx) to load the extension. I added the following before Module Settings

extension=mongo.so

It goes without saying you need to restart apache/nginx after changing the configuration.

Configuring Lumen

In your root lumen folder you can add it to your requirements with the following command.

composer require jenssegers/mongodb

From there you'll need to also load the MongodbServiceProvider before Facades or Eloquent is initialized.

$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class);$app->withFacades();$app->withEloquent();

For simplicity of organizing configuration I also created a config folder and a database.php config file. Since Lumen doesn't try to autoload or search this directory we have to tell it to load this config. I put the following line right before the loading the application routes.

$app->configure('database');

In database.php the mongodb driver requires a specific structure. I've included mysql in here as I use both, but if you're using mongo exclusively you can change default to mongodb and remove the mysql config.

return  [    'default' => 'mysql',    'connections' => [        'mysql' => [            'driver'    => 'mysql',            'host'      => env('DB_HOST', 'localhost'),            'database'  => env('DB_DATABASE', ''),            'username'  => env('DB_USERNAME', ''),            'password'  => env('DB_PASSWORD', ''),            'charset'   => 'utf8',            'collation' => 'utf8_unicode_ci',            'prefix'    => '',            'strict'    => false,        ],        'mongodb' => array(            'driver'   => 'mongodb',            'host'     => env('MONGODB_HOST', 'localhost'),            'port'     => env('MONGODB_PORT', 27017),            'username' => env('MONGODB_USERNAME', ''),            'password' => env('MONGODB_PASSWORD', ''),            'database' => env('MONGODB_DATABASE', ''),            'options' => array(                'db' => env('MONGODB_AUTHDATABASE', '') //Sets the auth DB            )        ),    ],];

With the configuration out of the way you can now create a model, as of writing this to create a model for mongo (check the github page) you can use the following as a base. You can ignore the $connection variable if mongo is your default driver.

<?phpnamespace App;use Jenssegers\Mongodb\Model as Eloquent;class Example extends Eloquent {    protected $connection = 'mongodb';    protected $collection = 'example';    protected $primaryKey = '_id';}

There you go, you should be able to interact with mongo normally, for the specifics of the driver check out the github page for documentation on it.

If this answer helped you could you mark it as the answer?


2016 (Update)

There is now a simple Doctrine MongoDB ODM Provider for the Lumen PHP framework.

composer require nordsoftware/lumen-doctrine-mongodb-odm

GitHub Source Code


Warning

jenssegers/mongodb is a Driver sitting on top of Illumante's Eloquent ORM.

Think of it: Eloquent ORM is primary made for SQL. And let's cut with the chase: The package is the reinvention of the wheel - as a side effect, major features are not supported. Besides that, the package is unstable and unmaintained.

Be aware, jenssegers/mongodb will vent your anger and frustration:

jenssegers/mongodb exploding issues and pr's


Just a change in @Sieabah user:instead: extension=mongo.sochoose: extension=mongodb.so