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:
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.getConnectionName()
to query within the active tenant databaseEvery 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:
Answering just one would be greatly appreciated. Thanks in advance!