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

查看:279
本文介绍了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  \Illuminate\Http\Request $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.

希望模式生成器,查询生成器和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天全站免登陆