15

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?

Hugo
  • 27,885
  • 8
  • 82
  • 98
Galin Denev
  • 153
  • 1
  • 1
  • 5
  • Have you heard of [Moloquent](http://jenssegers.be/blog/48/combining-laravel-and-mongodb)? The *develop* branch on the Github [repository](https://github.com/jenssegers/laravel-mongodb/blob/develop/composer.json) targets L5 so presumably **Lumen** also. Why not give a try to it? – menjaraz Jul 24 '15 at 12:41

3 Answers3

32

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.

<?php

    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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sieabah
  • 954
  • 10
  • 11
  • Thank you very much for the answer! It works! I almost had it done by this point, perhaps I've missed something. Brilliant answer! Thank you! – Galin Denev Jul 26 '15 at 07:32
  • @user743777 Glad to help! – Sieabah Jul 26 '15 at 17:00
  • I got a white screen with a status code of 200 after I followed this, And when I debug the $app instance in my app, I see that there is no db property set. What should I change there ? The app dies after Jennsengers Mongo registration because of the missing db in the app. – Cem Baykam Dec 16 '15 at 12:07
  • Warning: `jenssegers/mongodb` MongoDB Driver for Eloquent is unstable and unmaintained. – mate64 Aug 10 '16 at 11:02
  • I agree with mate64 here. don't use `jenssegers/mongodb`. use official `mongo-php-library`. it has PHP7 support also – Hadi Farnoud Aug 20 '16 at 07:25
10

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

mate64
  • 9,876
  • 17
  • 64
  • 96
  • 3
    2019 update . Jenssegers has 110 issues and 4.9k stars, while nordsoftware has 4 issues and 16 stars .... – Diego Favero Jun 20 '20 at 14:12
  • One would expect Object oriented way to handle nosql documents. jenssegers/mongodb is not suitable for large projects and sophisticated document schemas where you would want to create documents and embedded documents. doctrine-odm is far more better than this. However, someone needs to create a dedicated package for mongodb that fits properly in the laravel ecosystem – Ravish Jul 22 '22 at 07:30
-1

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

Community
  • 1
  • 1
RafaelPlantard
  • 117
  • 1
  • 7
  • 1
    Believe it depends on the PHP version, the official mongo installation is http://php.net/manual/en/mongo.installation.php – Sieabah Jul 07 '16 at 00:16