Files
ladill-link/app/Http/Controllers/Link/CustomDomainController.php
T
isaaccladandCursor 9516fcb9f3
Deploy Ladill Link / deploy (push) Successful in 37s
Expand Link app with analytics, settings, and custom domains.
Add Bitly-style branded domain support via Ladill Domains SSL API,
account analytics dashboard, settings page with default domain picker,
and fix SSO/dashboard issues from QR Plus template leftovers.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-27 12:16:34 +00:00

95 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers\Link;
use App\Http\Controllers\Controller;
use App\Models\LinkCustomDomain;
use App\Services\CustomDomain\DomainsSslClient;
use App\Services\CustomDomain\LinkCustomDomainService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class CustomDomainController extends Controller
{
public function __construct(
private readonly LinkCustomDomainService $service,
private readonly DomainsSslClient $domains,
) {}
public function index(Request $request): View
{
$account = ladill_account();
return view('links.domains.index', [
'domains' => $account->linkCustomDomains()->latest()->get(),
'enabled' => $this->service->enabled(),
'serverIp' => (string) config('customdomain.server_ip'),
'publicDomain' => (string) config('link.public_domain', 'ladl.link'),
]);
}
public function store(Request $request): RedirectResponse
{
abort_unless($this->service->enabled(), 404);
$data = $request->validate([
'host' => ['required', 'string', 'max:255', 'regex:/^(?!-)([a-z0-9-]+\.)+[a-z]{2,}$/i'],
'include_www' => ['nullable', 'boolean'],
'make_default' => ['nullable', 'boolean'],
]);
$host = strtolower(preg_replace('/^www\./', '', trim($data['host'])));
if (LinkCustomDomain::where('host', $host)->exists()) {
return back()->withErrors(['host' => 'That domain is already connected.']);
}
$domain = $this->service->attach(
$request->user(),
$host,
(bool) ($data['include_www'] ?? true),
(bool) ($data['make_default'] ?? false),
);
$this->domains->registerConnected(
$host,
(string) $request->user()->public_id,
'Ladill Link',
route('link.domains.index'),
);
return back()->with('success', "Domain added. Point an A record for {$host} to ".config('customdomain.server_ip').', then click Verify.');
}
public function verify(Request $request, LinkCustomDomain $customDomain): RedirectResponse
{
abort_unless($customDomain->user_id === $request->user()->id, 403);
[$ok, $message] = $this->service->verifyAndProvision($customDomain);
return back()->with($ok ? 'success' : 'error', $message);
}
public function makeDefault(Request $request, LinkCustomDomain $customDomain): RedirectResponse
{
abort_unless($customDomain->user_id === $request->user()->id, 403);
abort_unless($customDomain->isLive(), 422);
$this->service->setDefault($customDomain);
return back()->with('success', $customDomain->host.' is now your default short-link domain.');
}
public function destroy(Request $request, LinkCustomDomain $customDomain): RedirectResponse
{
abort_unless($customDomain->user_id === $request->user()->id, 403);
$host = $customDomain->host;
$customDomain->delete();
$this->domains->removeConnected($host);
return back()->with('success', 'Custom domain removed.');
}
}