Laravel Config::set 通过请求持久化? [英] Laravel Config::set Persist Through Requests?

查看:44
本文介绍了Laravel Config::set 通过请求持久化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在构建一个跟踪统计数据的 Web 应用程序.这些统计数据可以在不同公司之间变化.
我决定使用一个主数据库来存放所有登录凭据,每个公司都有一个单独的数据库.
用户登录后,他们将被重定向到此功能...

I have been building a web application that tracks statistics. These statistics can range between different companies.
I decided to use a main database to house all of the login credentials and each company gets a separate database to themselves.
After the user logins in they will be redirected to this function...

/**
* Redirects the user to the appropriate sub link
*/
public function redirect()
{
    Config::set('database.connections.club.database', Auth::user()->club->db_name);
    if (Auth::user()->person->role_id == 4) {
        return Redirect::to('employee/club_manager/home');
    }
}

此函数将称为俱乐部数据库的辅助数据库配置设置为与用户关联的俱乐部相同.

This function sets the secondary database configuration called club's database equal to the user's associated club.

但是,当我稍后尝试访问此配置时...

However when I try to access this configuration later on...

/**
* Handle an incoming request.
*
* @param  IlluminateHttpRequest $request
* @param  Closure $next
* @return mixed`enter code here`
*/
public function handle($request, Closure $next)
{
    dd(Config::get('database.connections.club.database'));
    if (Auth::user()->role->id == 4)
        return $next($request);
}

dd() 将返回 '',这是我在 database.php 文件中设置的默认设置.

The dd() will return '', which is the default setting I set in the database.php file.

我的问题是,如何跨请求保存配置设置,因为 Config::set 似乎只保存单个请求的设置.

推荐答案

在 OP 的要求下,我提交了第二个答案作为不同的方法.

After OP's request, I submit a second answer as a different approach.

如果这只是为了提供数据库连接的详细信息,您可以在单个 Laravel 应用程序中对多个数据库连接使用以下方法.

If this is only intented for database connection details, you could use the following approach with multiple database connections in a single Laravel application.

app/config/database.php 中,您定义了两个不同的连接.一个用于存储 users 表的主数据库,一个用于每个客户端实例:

In app/config/database.php you define two different connections. One for your main database where your users table is stored and one dynamic for the per-client instance:

'default' => 'mysql_main',
'connections' => [

    'mysql_main' => [
        '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' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

    'mysql_company' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => Auth::user()->club->db_name,
        'username' => Auth::user()->club->db_user,
        'password' => Auth::user()->club->db_pass,
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

],

正如您所看到的,您实际上不需要从配置文件中读取第二个连接的连接详细信息(它仍将用于获取主机、端口和主数据库的任何常见详细信息),您可以传递不同的来自您登录用户的动态连接详细信息.但是,这将要求您提前为每个用户创建所有数据库.

As you can see you dont actually need to read the connection details of the second connection from the config file (it would still be used to get host, port and any common details with your main database), you can pass the different connection details dynamically from your logged-in user. However this will require you to have created all the databases for every user in advance.

在您设法连接到两个数据库后,您必须在执行查询的任何时候选择活动连接.

After you have managed to connect to both databases, you will have to select the active connection anytime you execute a query.

希望 Schema Builder、Query Builder 和 Eloquent 支持开箱即用:

Hopefully Schema Builder, Query Builder and Eloquent support this out of the box:

Shema Builder

Schema::connection('mysql_company')->create('statisticA', function($table)
{
    $table->increments('id'):
});

查询构建器

$companyStatA = DB::connection('mysql_company')->select(...);

雄辩模型

class StatisticA extends Eloquent {

    protected $connection = 'mysql_company';

}

控制器

class SomeController extends BaseController {

    public function someMethod()
    {
        $statA = new StatisticA;

        $statA->setConnection('mysql_company');

        $something = $statA->find(1);

        return $something;
    }

}

然而,定义存储在不同数据库中的模型之间的关系会很棘手.这是可能的,但这在很大程度上取决于您的数据库驱动程序和设置.

However what would be tricky is to define relationships between models stored in different databases. It is be possible but it strongly depends on your database driver and settings.

我怀疑它能否在不同类型的数据库(例如 MySQL 作为您的主数据库和 PostgreSQL 作为您的公司数据库)之间正常运行,所以我建议您坚持使用 MySQL,看看效果如何.

I doubt it would function correctly between different types of databases (eg MySQL as your main database and PostgreSQL for you company databases), so I'd suggest you to stick with MySQL and see how this goes.

这篇关于Laravel Config::set 通过请求持久化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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