ringierimu / multi-tenant
Laravel Multi-Tenancy package
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/ringierimu/multi-tenant
Requires
- php: >=7.1.3
- illuminate/support: ^5.6 || ^5.7 || ^5.8
Requires (Dev)
- orchestra/testbench: ^3.5
- phpunit/phpunit: ^6.0 || ^7.0
This package is not auto-updated.
Last update: 2024-02-18 16:54:25 UTC
README
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.
Installation
$ composer require ringierimu/multi-tenant
Run migrations
$ php artisan migrate
Configuration
- Open
kernel.phplocated inside your Http directory and addRingierimu\MultiTenant\Http\Middleware\TenantMiddleware\TenantMiddleware::classto 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
TenantDependableTraitto 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
domainstable 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 ]);
Features
Tenants Resolver
- The
TenantMiddleware::classresolve the tenants through http request. It uses the request domain to querydomainstable. - To get instance of the resolved Tenant, you can use dependency injection to inject
TenantManagerclass or use laravel IOC to return existing instance ofTenantManagerclass.
<?php $tenantManager = app(Ringierimu\MultiTenant\TenantManager::class); echo $tenantManager->getDomain();
<?php use Ringierimu\MultiTenant\TenantManager; public function login(TenantManager $tenantManager) { echo $tenantManager->getDomain(); }
Tenants App Config
- To add a custom configuration per per Tenants, add directory
tenantsinside the laravel default config directory with the tenantaliaseskey as a subdirectory. eg.config/tenants/rg/app.php.
NB.aliaseskey must be the same as the Tenant aliases key set ondomainstable. - Any config keys found inside tenants directory will override any existing key of laravel default config.