How to use orchestral/tenanti in Laravel 5 to build a multi tenant application with multiple databases?

前端 未结 2 1504
不知归路
不知归路 2021-02-04 08:19

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

2条回答
  •  心在旅途
    2021-02-04 09:01

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

    Migration

    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?

    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 however will use the migration path set under the "driver" configuration. e.g:

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

    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

    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".

    Authentication or Accessing DB

    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 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.

    Let say you have the following config:

     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' => [ ... ],
    ];
    

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

    getKey()}";
    
                return $template;
            });
        }
    }
    

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

    So how does Tenanti detect which user if active?

    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}');
    

提交回复
热议问题