流明和MongoDB? [英] Lumen and MongoDB?

查看:95
本文介绍了流明和MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能将mongodb连接设置包含到流明框架中.从我看到的结果来看,config/database.php是在lumen包内部加载的.有什么办法可以扩展它以包含mongodb连接设置吗?

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?

推荐答案

我们实际上是在一个大型项目中使用Lumen,Laravel,Mongo和MySQL,因此我可以帮助您完成这个项目.假设您要雄辩地使用MongoDB,而不是原始的MongoClient.您可以在jenssegers的此处找到我正在使用的库.

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.

首先,您需要安装PHP的依赖项才能与mongo进行交互.可以在 PHP文档中找到安装mongo扩展程序的详细信息.

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.

之后,您必须编辑平台的php.ini文件(apache/cli/nginx)以加载扩展.我在模块设置

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

不用说,更改配置后需要重新启动apache/nginx.

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

在根管腔文件夹中,可以使用以下命令将其添加到您的要求中.

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

composer require jenssegers/mongodb

在初始化Facades或Eloquent之前,您还需要 从那里加载MongodbServiceProvider.

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();

为了简化组织配置,我还创建了一个config文件夹和一个database.php配置文件.由于Lumen不会尝试自动加载或搜索此目录,因此我们必须告诉它加载此配置.我在加载应用程序路由之前将以下行放置.

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');

在database.php中,mongodb驱动程序需要特定的结构.我在这两个地方都使用了mysql,但是如果您只使用mongo,则可以将default更改为mongodb并删除mysql配置.

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
            )
        ),

    ],
];

通过配置,您现在可以创建模型,在编写代码以创建mongo模型时(请检查github页面),您可以将以下内容用作基础.如果mongo是默认驱动程序,则可以忽略$ connection变量.

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';
}

到那里,您应该能够与mongo正常交互,有关驱动程序的详细信息,请查看github页面以获取有关其文档的信息.

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?

这篇关于流明和MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆