PHP code example of socialdept / tenant-domains

1. Go to this page and download the library: Download socialdept/tenant-domains 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/ */

    

socialdept / tenant-domains example snippets


use SocialDept\TenantDomains\Facades\TenantDomains;

$domain = $publication->domains()->create(['domain' => 'blog.example.com']);

TenantDomains::ensureDelegationId($domain);

// Everything the tenant has to create, ready to render.
$instructions = TenantDomains::instructionsFor($domain)->toArray();

// ['type' => 'CNAME', 'name' => 'blog',                'value' => 'to.yourapp.com']
// ['type' => 'TXT',   'name' => '_verify.blog',        'value' => 'verification=…']
// ['type' => 'CNAME', 'name' => '_acme-challenge.blog','value' => 'k7m2q9.acme.yourapp.com']

if (TenantDomains::verifyOwnership($domain) && TenantDomains::verifyRouting($domain)->verified) {
    TenantDomains::publish($domain);   // the edge now serves it and gets a certificate
}

// A different label under your own domain
'cname_target' => 'ingress.{platform}',

// An existing load balancer on a completely separate domain
'cname_target' => 'lb-7a2.eu-west.example.net',

// A CDN hostname you already publish
'cname_target' => 'customers.yourcdn.example',

use SocialDept\TenantDomains\Contracts\ProvidesOwnershipToken;

class Publication extends Model implements ProvidesOwnershipToken
{
    public function domainOwnershipToken(): string
    {
        return $this->domain_token;
    }
}

use SocialDept\TenantDomains\Models\Concerns\IsCustomDomain;
use Stancl\Tenancy\Database\Models\Domain as BaseDomain;

class Domain extends BaseDomain
{
    use IsCustomDomain;

    protected function casts(): array
    {
        return [...parent::casts(), ...$this->customDomainCasts()];
    }
}

'authority_endpoint' => [
    'path' => 'internal/tls/authorize',
    'middleware' => ['internal-only'],
],

// config/tenant-domains.php
'authority_endpoint' => ['enabled' => false],

// routes/api.php
Route::get('v2/tls/authorize', CertificateAuthorityController::class);

Domains::authorizeCertificatesUsing(
    fn (DomainName $host) => Domain::where('domain', $host->parent())->verified()->exists(),
);

Schedule::command('domains:reconcile')->everyFiveMinutes();

'drivers' => ['zone' => 'cloudflare', 'ingress' => 'caddy'],

app(DriverManager::class)->extendIngress('traefik', fn ($app) => new TraefikDriver(...));

$resolver = new ArrayResolver([
    'blog.example.com' => ['CNAME' => 'to.yourapp.com'],
    '_verify.blog.example.com' => ['TXT' => 'verification=tok_123'],
]);

$ingress = new FakeIngressDriver();
$this->app->instance(IngressDriver::class, $ingress);

// ...

$ingress->assertPublished('blog.example.com', delegatedTo: 'k7m2q9.acme.yourapp.com');
bash
php artisan vendor:publish --tag=tenant-domains-config

# A fresh domains table...
php artisan vendor:publish --tag=tenant-domains-migrations

# ...or, if you already have one (stancl/tenancy does), just the extra columns:
php artisan vendor:publish --tag=tenant-domains-migrations-columns

php artisan migrate