1. Go to this page and download the library: Download rasuvaeff/yii3-tenancy library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rasuvaeff / yii3-tenancy example snippets
use Rasuvaeff\Yii3Tenancy\ConfigTenantProvider;
use Rasuvaeff\Yii3Tenancy\HeaderTenantResolver;
use Rasuvaeff\Yii3Tenancy\RequestCurrentTenant;
use Rasuvaeff\Yii3Tenancy\TenantResolutionMiddleware;
$middleware = new TenantResolutionMiddleware(
resolver: new HeaderTenantResolver(), // X-Tenant-Id
provider: new ConfigTenantProvider([
'acme' => ['name' => 'Acme Inc', 'attributes' => ['plan' => 'pro']],
]),
currentTenant: $requestCurrentTenant, // shared RequestCurrentTenant
responseFactory: $psr17Factory,
);
use Rasuvaeff\Yii3Tenancy\CurrentTenant;
final readonly class InvoiceService
{
public function __construct(private CurrentTenant $currentTenant) {}
public function create(): void
{
$tenantId = $this->currentTenant->get()->id; // throws if unresolved
$plan = $this->currentTenant->get()->attributes['plan'] ?? 'free';
}
}
use Rasuvaeff\Yii3Tenancy\TenantScopedCache;
$cache = new TenantScopedCache($psr16Cache, $currentTenant);
$cache->set('report', $data); // stored as "t.acme.report"
// config/common/di/tenancy.php
use Rasuvaeff\Yii3Tenancy\ConfigTenantProvider;
use Rasuvaeff\Yii3Tenancy\TenantProvider;
return [
TenantProvider::class => static fn (): TenantProvider => new ConfigTenantProvider([
'acme' => ['name' => 'Acme Inc'],
]),
];