如何在Laravel 5中使用Orchestral/tenanti构建具有多个数据库的多租户应用程序? [英] How to use orchestral/tenanti in Laravel 5 to build a multi tenant application with multiple databases?

查看:160
本文介绍了如何在Laravel 5中使用Orchestral/tenanti构建具有多个数据库的多租户应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel 5进行构建和应用.应该是使用多个数据库的多租户数据库体系结构.我的雇主出于安全目的要求这样做.

I am trying to build and application using Laravel 5. It is supposed to be a multi tenant database architecture using multiple databases. My employer requires this for security purposes.

我尝试手动管理主要的数据库迁移和租户迁移,但是失败了.因此,我决定采用Laravel特定软件包的帮助,这应该是我所需要的.

I have tried manually managing the main DB migrations and the Tenant migrations but failed. So I decided to take the help of a Laravel specific package which is supposedly what I require.

Tenanti 提供了一种解决目标的方法,但问题是我是新手开发人员,无法完全了解如何在我的应用程序中使用它.

Tenanti provides a way to have my purpose solved but the problem is that me being a novice developer, am not able to fully understand how to use it in my application.

我相信我已经正确安装了它:

I have installed it correctly I believe doing:

composer require "orchestra/tenanti=~3.0"

在配置应用文件中添加这些提供程序和别名:

Adding these providers and aliases in the config app file:

'providers' => [

    // ...
    Orchestra\Tenanti\TenantiServiceProvider::class,
    Orchestra\Tenanti\CommandServiceProvider::class,
],

'aliases' => [

    'Tenanti' => Orchestra\Support\Facades\Tenanti::class,

],

最后发布配置并根据多个数据库的文档进行调整:

Finally publishing the config and tweaking it according to the documentation for multiple databases:

php artisan vendor:publish

return [
    'drivers' => [
        'user' => [
            'model'     => App\User::class,
            'migration' => 'tenant_migrations',
            'path'      => database_path('tenanti/user'),
        ],
    ],
];

这时我仍然不清楚下一步该怎么做?

At this point I am still blurry what to do next?

我的疑问如下:

  1. 迁移文件将在哪里生成和存储?我的意思是我的应用程序中显然有两种数据库.一组文件用于主数据库,它将存储所有租户信息,而其他文件将用于租户数据库.那么这些将如何存储在哪里?
  2. 我在文档中经常看到驱动程序"一词,但我不确定到底是什么驱动程序.
  3. 我将如何处理应用程序的身份验证?我的意思是,每当租户登录时,我都必须确保与数据库的连接是动态变化的.我该怎么做?
  4. 我试图浏览软件包本身的存储库,并弄明白其中的代码,但徒劳无功.当涉及外观,命令总线,服务提供者等设计模式时,我不是很好,这就是为什么我无法理解程序包的流程或无法理解程序包的原因.

我试图运行软件包附带的一些工匠命令,例如:

I tried to run some of the artisan commands which come with the package like:

php artisan tenanti:install {driver}
php artisan tenanti:make {driver} {name}

但是我遇到这样的错误:

But I am getting an error like so:

[InvalidArgumentException]数据库连接 [租户]不可用.

[InvalidArgumentException] Database connection [tenants] is not available.

在哪里可以找到了解如何进行此操作的资源?

Where can I find the resources to understand how to proceed with this?

推荐答案

+1到@morphatic答案,它对大多数内容都非常准确.

+1 to @morphatic answer, it quiet accurate on most of the stuff.

一组文件用于主数据库,该文件将存储所有租户信息,而其他文件将用于该租户数据库.那么这些将如何存储在哪里?

One set of files is for the main DB which will store all the tenant information and the other files will be for the tenant DB. So how and where will these be stored?

对于主数据库,您应该能够使用默认的database/migration并使用php artisan make:migrationphp artisan migrate.

For your main database you should be able to use the default database/migration and utilize php artisan make:migration and php artisan migrate.

Tenanti将使用在驱动程序"配置下设置的迁移路径.例如:

Tenanti however will use the migration path set under the "driver" configuration. e.g:

'path' => database_path('tenanti/user'),

在这种情况下,将从database/tenanti/user创建/迁移迁移(您可以选择其他文件夹,它将使用该文件夹).设置完成后,您可以通过php artisan tenanti:make user create_blogs_table为用户租户创建新的迁移文件(例如),并通过php artisan tenanti:migrate user运行迁移(请参阅Laravel迁移命令和Tenanti之间的相似之处?).

In this case the migration will be created/migrated from database/tenanti/user (you can choose other folder and it will use that folder). Once you set this up you can create new migration file for the user tenant via php artisan tenanti:make user create_blogs_table (as an example) and run migration via php artisan tenanti:migrate user (see the similarity between Laravel migration command and Tenanti?).

驱动程序只是一个租户的分组,您可以按用户,公司或团队等对它进行分组.每个项目可能需要多个类型的分组,否则大多数情况下,您只能使用单个组"或驱动程序".

Driver is just the grouping of a tenant, you maybe grouping it by users, companies, or team etc. And there is possibility that you may require more than one type of group per project, otherwise most of the time you only be using single "group" or "driver".

我将如何处理应用程序的身份验证?我的意思是,每当租户登录时,我都必须确保与数据库的连接是动态变化的.我该怎么做?

How will I handle the authentication for the application? I mean whenever a tenant logs in, I will have to make sure the connection to the database changes dynamically. How will I accomplish this?

首先,您需要考虑如何计划区分每个租户.大多数时候,我会看到人们倾向于选择子域.因此,在这种情况下,您需要使用中间件检查子域是否属于任何用户(通过查询主数据库),然后连接到属于该用户的数据库.

First of all, you need to consider how you're planning to distinguish each tenant. Most of the time I would see people tend to opt for subdomain. So in this case you need to check if the subdomain belongs to any of the user (by querying the main database) using a middleware and then connect to the database that belongs to the user.

Tenanti不会管理该过程的那部分,因为每个人在这方面都有不同的风格,但是我们确实提供了代码以从基本数据库配置动态连接到您的数据库租户.

Tenanti doesn't manage that part of the process, because everyone has different style on that aspect, but we do provide a code to dynamically connect to your database tenant from a base database configuration.

假设您具有以下配置:

<?php

return [
    'fetch' => PDO::FETCH_CLASS,
    'default' => 'primary',
    'connections' => [
        'primary' => [
            //
        ],
        'tenants' => [
                'driver'    => 'mysql',
                'host'      => 'dbhost',     // for user with id=1
                'username'  => 'dbusername', // for user with id=1
                'password'  => 'dbpassword', // for user with id=1
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
            ],
       ],
    ],
    'migrations' => 'migrations',
    'redis' => [ ... ],
];

您可以按照 https://github.com/orchestral中可用的步骤进行操作/tenanti#multi-database-connection-setup 并添加以下代码.

You can follow the step available in https://github.com/orchestral/tenanti#multi-database-connection-setup and add the following code.

<?php namespace App\Providers;

use Orchestra\Support\Facades\Tenanti;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Tenanti::setupMultiDatabase('tenants', function (User $entity, array $template) {
            $template['database'] = "tenant_{$entity->getKey()}";

            return $template;
        });
    }
}

这将确保您为用户= 1使用tenant_1数据库,为用户= 2使用tenant_2数据库,依此类推.

This would ensure that you be using tenant_1 database for user=1, tenant_2 database for user=2 and so on.

这是您需要在中间件中添加逻辑的地方.

This is where you need to add logic in your middleware.

$user = App\User::whereSubdomain($request->route()->parameter('tenant'))->first();

Tenanti::driver('user')->asDefaultDatabase($user, 'tenants_{id}');

这篇关于如何在Laravel 5中使用Orchestral/tenanti构建具有多个数据库的多租户应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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