Lumen and MongoDB?

无人久伴 提交于 2019-11-28 20:36:59

问题


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?


回答1:


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?




回答2:


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 mongodb features are not supported. Besides that, the package is unstable and unmaintained.

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




回答3:


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



来源:https://stackoverflow.com/questions/31547827/lumen-and-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!