laravel 流明和MongoDB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31547827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lumen and MongoDB?
提问by Galin Denev
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?
是否有可能将 mongodb 连接设置包含到 lumen 框架中。从我看到的 config/database.php 是在 lumen 包内部加载的。有没有办法以某种方式扩展它以包含 mongodb 连接设置?
回答by Sieabah
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.
我们实际上在一个巨大的项目中使用 Lumen、Laravel、Mongo 和 MySQL,所以我可以帮助你完成这个项目。假设您想将 MongoDB 与 eloquent 一起使用,而不是与原始的 MongoClient 一起使用。您可以从 jenssegers此处找到我正在使用的库。
Install MongoDB Extension
安装 MongoDB 扩展
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 与 mongo 交互的依赖项。安装 mongo 扩展的细节可以在PHP 文档中找到。
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
之后,您必须编辑平台 (apache/cli/nginx) 的 php.ini 文件以加载扩展。我在模块设置之前添加了以下内容
extension=mongo.so
It goes without saying you need to restart apache/nginx after changing the configuration.
不用说,更改配置后需要重新启动 apache/nginx。
Configuring Lumen
配置流明
In your root lumen folder you can add it to your requirements with the following command.
在您的根 lumen 文件夹中,您可以使用以下命令将其添加到您的要求中。
composer require jenssegers/mongodb
From there you'll need to also load the MongodbServiceProvider beforeFacades or Eloquent is initialized.
从那里你还需要在Facades 或Eloquent初始化之前加载 MongodbServiceProvider 。
$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.
为了组织配置的简单性,我还创建了一个 config 文件夹和一个 database.php 配置文件。由于 Lumen 不会尝试自动加载或搜索此目录,因此我们必须告诉它加载此配置。我在加载应用程序路由之前放置了以下行。
$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.
在 database.php 中,mongodb 驱动程序需要特定的结构。我在此处包含了 mysql,因为我同时使用了两者,但是如果您只使用 mongo,则可以将默认值更改为 mongodb 并删除 mysql 配置。
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.
通过配置,您现在可以创建模型,在编写此代码以创建 mongo 模型(检查 github 页面)时,您可以使用以下内容作为基础。如果 mongo 是您的默认驱动程序,您可以忽略 $connection 变量。
<?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.
好了,您应该能够正常与 mongo 交互,有关驱动程序的详细信息,请查看 github 页面以获取有关它的文档。
If this answer helped you could you mark it as the answer?
如果这个答案对您有帮助,您可以将其标记为答案吗?
回答by mate64
2016 (Update)
2016(更新)
There is now a simple Doctrine MongoDB ODMProviderfor the Lumen PHP framework.
现在有一个用于Lumen PHP 框架的简单Doctrine MongoDB ODM Provider。
composer require nordsoftware/lumen-doctrine-mongodb-odm
Warning
警告
jenssegers/mongodb
is a Driversitting on top of Illumante's Eloquent ORM.
jenssegers/mongodb
是司机坐在上面Illumante雄辩的ORM。
Think of it: Eloquent ORMis primary made for SQL. And let's cut with the chase: The package is the reinvention of the wheel - as a side effect, major mongodbfeatures are not supported. Besides that, the package is unstable and unmaintained.
想想吧:雄辩的ORM是主要为SQL做。让我们切入正题:该包是轮子的重新发明 - 作为副作用,不支持主要的mongodb功能。除此之外,该包不稳定且无需维护。
Be aware, jenssegers/mongodb
will vent your anger and frustration:
注意,jenssegers/mongodb
会发泄你的愤怒和沮丧: