Lumen and MongoDB?

Galin Denev picture Galin Denev · Jul 21, 2015 · Viewed 11.6k times · Source

Is it somehow possible to include the mongodb connection settings into a lumen framework. As from what I saw the config/database.php is loaded internally in the lumen package. Is there a way to extend it somehow to include the mongodb connection settings?

Answer

Sieabah picture Sieabah · Jul 24, 2015

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.

<?php

namespace 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?