流明-在运行时创建数据库连接 [英] Lumen - Create database connection at runtime

查看:62
本文介绍了流明-在运行时创建数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Lumen项目中,我需要在运行时创建数据库连接,但是每次尝试使用最近创建的连接时,都会出现未配置数据库"错误.

In a Lumen project, I need to create database connections on runtime, but I keep getting a "Database [...] not configured" error, each time I try to use a recently created connection.

这是我在route.php上的测试代码:

This is my test code on routes.php:

<?php

$app->get('/', function () use ($app) {

    $config = $app->make('config');
    $config->set('database.connections.retail_db', [
        'driver'   => 'pgsql',
        'host'     => env('RETAIL_DB_HOST', 'localhost'),
        'port'     => env('RETAIL_DB_PORT', 5432),
        'database' => env('RETAIL_DB_DATABASE', 'forge'),
        'username' => env('RETAIL_DB_USERNAME', 'forge'),
        'password' => env('RETAIL_DB_PASSWORD', ''),
        'charset'  => env('RETAIL_DB_CHARSET', 'utf8'),
        'prefix'   => env('RETAIL_DB_PREFIX', ''),
        'schema'   => env('RETAIL_DB_SCHEMA', 'public'),
    ]);
    return app('db')->connection('retail_db')->select("SELECT * FROM users");

});

该代码应该可以在Laravel上运行,但是我找不到有关流明的任何信息.

This code is supposed to work on Laravel, but I can't find any information regarding Lumen.

我正在使用最新的Lumen版本.

I'm using the latest Lumen version.

推荐答案

您要使用的方法存在一个主要问题:

There is one main problem with the method you are going for:

您没有初始化任何配置对象.默认情况下,Lumen没有传统的配置对象集,除非您在根文件夹中创建config目录.

You did not initialize any configuration object. Lumen by default has no traditional config object set, until you create a config directory in your root folder.

流明配置文档中所述:

Lumen框架的所有配置选项都存储在.env文件中.

All of the configuration options for the Lumen framework are stored in the .env file.

您要使用的方法需要Laravel中使用的传统配置对象.

The approach you are going for requires the traditional config object as used in Laravel.

要使该对象和新的retail_db数据库连接正常工作:

To get that object and your new retail_db database connection working:

  • 在项目根目录中创建一个config文件夹
  • 将文件vendor/laravel/lumen-framework/config/database.php复制到此配置文件夹
  • $app->configure('database');初始化bootstrap/app.php中的数据库配置对象(将其放在第28行)
  • Create a config folder in your project root
  • Copy the file vendor/laravel/lumen-framework/config/database.php to this config folder
  • Initialize the database configuration object in your bootstrap/app.php with $app->configure('database'); (put it at line 28)

您的文件夹结构现在看起来像这样:

Your folder structure looks like this now:

├── app
├── bootstrap
├── config
   └── database.php
├── database
├── public
├── resources
├── storage
├── tests
└── vendor

当然,您可以通过注释或完全删除它们来从app/config/database.php中的连接数组中删除不需要的那些连接.

Of course you can remove those connections you don't need from the connections array in app/config/database.php by commenting or removing them completely.

app/config/database.php

'connections' => [

        /*'testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
        ],*/

        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => env('DB_DATABASE', base_path('database/database.sqlite')),
            'prefix'   => env('DB_PREFIX', ''),
        ],

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'port'      => env('DB_PORT', 3306),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => env('DB_CHARSET', 'utf8'),
            'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
            'prefix'    => env('DB_PREFIX', ''),
            'timezone'  => env('DB_TIMEZONE', '+00:00'),
            'strict'    => env('DB_STRICT_MODE', false),
        ],
]

具有更改的 bootstrap/app.php :

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

//$app->withFacades();
// $app->withEloquent();

$app->configure('database');

现在您可以使用routes.php中已经存在的代码.

Now you can use the code you already have in your routes.php.

要删除您的retail_db连接,只需将其设置为null:

To delete your retail_db connection, just set it to null:

$config->set('database.connections.retail_db', null);

这篇关于流明-在运行时创建数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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