dtt27783 2015-12-19 20:31
浏览 66

Laravel多租户应用方法

I'm trying to build a multi-tenant application with Laravel 5. I'm aware of already existing packages like Hyn and AuraEQ, but I still find these hard to understand and I want to make my own (simplified) version. This way I exactly know what is going on under the hood. I've read pretty much everything on the internet before starting, but there is not much information available on this subject.

The approach I have in mind is pretty straightforward:

  • Define the tenants within app/config/tenant.php and the coresponding database connection within app/config/database.php. I think storing the tenants in a 'master' database is overkill.
  • Register a Service provider to enable multi-tentancy
  • Register a Singleton in the IoC container which detects the (active) tenant
  • Extend all Eloquent models and override getConnectionName() to query within the active tenant database

Every website (tenant) has it's own folder within the storage path. Let's say I have a website named 'coding.com', the path will be something like: app/storage/tenants/coding-com. The tenant currently has it's own views and routes.

So far so good, this is the code:

class TenantServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app->singleton('Tenant', function () {
            $tenants = config('tenant.hosts');
            $host = Request::server('HTTP_HOST');

            if(array_key_exists($host, $tenants)) {
                return $tenants[$host];
            }

            return null;
        });

        $tenant = $this->app->make('Tenant');
        $directory = tenant_path($tenant);

        if($tenant && is_dir($directory)) {
            // Load views from the current tenant directory
            $this->app['view']->addLocation($directory . '/views');

            // Load optional routes from the current tenant directory
            if(file_exists($directory . '/routes.php')) require_once $directory . '/routes.php';
        }

        // Load base views, these will be overridden by tenant views with the same name
        $this->app['view']->addLocation(realpath(base_path('resources/views')));
    }
}

Defining the tentants:

return [

    /**
     * Base path of the tenant's directory.
     */
    'path' => storage_path('tenants'),

    /**
     * This is where the tenants are defined.
     */
    'hosts' => [
        'coding.com' => [
            'id'    => 'coding-com', // Connection within config/database.php is also named coding-com
            'https' => false,
        ],
        'other.com' => [
            'id'    => 'other-nl',
            'https' => false,
        ]
    ]

];

And I have to extend every Eloquent model with a class named 'TentantModel', which overrides the getConnectionName() method:

/**
 * Get the current connection name for the model.
 *
 * @return string
 */
public function getConnectionName()
{
    $tenant = app()->make('Tenant');

    if($tenant) {
        return array_get($tenant, 'id');
    }

    return parent::getConnectionName();
}

I apologize for the big chunks of code, but this makes the idea a lot easier to understand.

This brings me to a few questions:

  • How would I manage assets individually for each tenant?
  • Does the use of a Singleton make sence for this approach? It is the only thing I could come up with to have some sort of global variable and it works fine, but I doubt if Singleton's are meant for this kind of stuff.
  • Are there any downsides on this approach, things I didn't take into account when thinking this over?

Answering just one would be greatly appreciated. Thanks in advance!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题