The term multi-tenant refers to a software architecture in which a single instance of software runs on a server and serves multiple tenants. A tenant is a group of users who share a common access with specific privileges to the software instance. With a multi-tenant architecture, a software application is designed to provide every tenant a dedicated share of the instance - including its data, configuration, user management, tenant individual functionality and non-functional properties.
$ composer require ringierimu/multi-tenant
Run migrations
$ php artisan migrate
- Open
kernel.php
located inside your Http directory and addRingierimu\MultiTenant\Http\Middleware\TenantMiddleware\TenantMiddleware::class
to your global http middleware$middleware
.
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
...
TenantMiddleware::class
];
- Add
TenantDependableTrait
to your model class to support workflow
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Ringierimu\MultiTenant\Traits\TenantDependableTrait;
/**
* Class Post
* @package App
*/
class Post extends Model
{
use TenantDependableTrait;
}
- Create seeder for the
domains
table to populate table with data and run your seeder.
eg.
<?php
use Illuminate\Support\Facades\DB;
DB::table('domains')->insert([
'title' => 'Ringier',
'host' => 'ringier.test',
'alieses' => 'rg',
'country_id' => 1
]);
- The
TenantMiddleware::class
resolve the tenants through http request. It uses the request domain to querydomains
table. - To get instance of the resolved Tenant, you can use dependency injection to inject
TenantManager
class or use laravel IOC to return existing instance ofTenantManager
class.
<?php
$tenantManager = app(Ringierimu\MultiTenant\TenantManager::class);
echo $tenantManager->getDomain();
<?php
use Ringierimu\MultiTenant\TenantManager;
public function login(TenantManager $tenantManager)
{
echo $tenantManager->getDomain();
}
- To add a custom configuration per per Tenants, add directory
tenants
inside the laravel default config directory with the tenantaliases
key as a subdirectory. eg.config/tenants/rg/app.php
.
NB.aliases
key must be the same as the Tenant aliases key set ondomains
table. - Any config keys found inside tenants directory will override any existing key of laravel default config.