24
How Do I

Use public links

Generate tenant-aware public URLs from the backend.

public_url is a read-only value resolved by the backend. Use it when you need a shareable link that depends on the current owner or request context.

Backend pattern

  • define a resolver that looks at the current owner context
  • derive the public base domain from that owner
  • return the final URL at response time

Concrete multi-tenant example

If each tenant owns its own public domain, keep the domain decision in the backend resolver and let the client consume the returned public_url.

1. Register a resolver

config/formforge.php
'http' => [
    'public_link' => [
        'resolver' => App\FormForge\PublicLinks\TenantPublicLinkResolver::class,
    ],
],

2. Resolve the domain from the owner

app/FormForge/PublicLinks/TenantPublicLinkResolver.php
<?php

declare(strict_types=1);

namespace App\FormForge\PublicLinks;

use EvanSchleret\FormForge\Support\FormPublicLinkResolver;
use Illuminate\Http\Request;

class TenantPublicLinkResolver implements FormPublicLinkResolver
{
    public function resolve(array $context, Request $request): ?string
    {
        $key = trim((string) ($context['key'] ?? ''));

        if ($key === '') {
            return null;
        }

        $ownerType = (string) ($context['owner_type'] ?? '');
        $ownerId = $context['owner_id'] ?? null;

        $tenant = match ($ownerType) {
            'App\\Models\\Team' => \App\Models\Team::query()->find($ownerId),
            'App\\Models\\Account' => \App\Models\Account::query()->find($ownerId),
            default => null,
        };

        $host = is_string($tenant?->public_domain) && trim($tenant->public_domain) !== ''
            ? trim($tenant->public_domain)
            : $request->getHost();

        return sprintf('https://%s/api/formforge/v1/forms/%s', $host, $key);
    }
}

3. Consume the resolved URL

The HTTP resource returns public_url automatically when the resolver is configured. Your frontend or app code can use that value directly instead of rebuilding the domain itself.

Why this matters

  • multi-subdomain setups need a request-aware base domain
  • different tenants can expose the same form key on different hosts
  • the client should never hardcode the final domain
If your application uses multiple top-level domains or subdomains, resolve the base domain from the owner relation or attribute on the backend and keep the link generation deterministic there. A common pattern is to store a public_domain or forms_domain attribute on the tenant model and use it here.