laravel - Lumen and MongoDB? -


is somehow possible include mongodb connection settings lumen framework. saw config/database.php loaded internally in lumen package. there way extend somehow include mongodb connection settings?

we're using lumen, laravel, mongo, , mysql in 1 giant project can through one. assuming want use mongodb eloquent instead of raw mongoclient. can find library i'm using jenssegers here.

install mongodb extension

firstly you'll need install dependencies php interact mongo. specifics installing mongo extension can found on php documentation.

after you'll have edit php.ini files platforms (apache/cli/nginx) load extension. added following before module settings

extension=mongo.so 

it goes without saying need restart apache/nginx after changing configuration.

configuring lumen

in root lumen folder can add requirements following command.

composer require jenssegers/mongodb 

from there you'll need load mongodbserviceprovider before facades or eloquent initialized.

$app->register(jenssegers\mongodb\mongodbserviceprovider::class);  $app->withfacades();  $app->witheloquent(); 

for simplicity of organizing configuration created config folder , database.php config file. since lumen doesn't try autoload or search directory have tell load config. put following line right before loading application routes.

$app->configure('database'); 

in database.php mongodb driver requires specific structure. i've included mysql in here use both, if you're using mongo exclusively can change default mongodb , remove 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 auth db             )         ),      ], ]; 

with configuration out of way can create model, of writing create model mongo (check github page) can use following base. can ignore $connection variable if mongo default driver.

<?php  namespace app;  use jenssegers\mongodb\model eloquent;  class example extends eloquent  {     protected $connection = 'mongodb';     protected $collection = 'example';     protected $primarykey = '_id'; } 

there go, should able interact mongo normally, specifics of driver check out github page documentation on it.

if answer helped mark answer?


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -