Make Ladill Link short URLs fully dynamic at click time.
Deploy Ladill Link / deploy (push) Successful in 1m55s
Deploy Ladill Link / deploy (push) Successful in 1m55s
Forward query strings and path suffixes to the live destination, resolve public hosts from config/custom domains instead of hardcoding ladl.link, and keep proxy Location rewrites on the visitor host.
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ShortLink extends Model
|
||||
{
|
||||
@@ -44,6 +45,10 @@ class ShortLink extends Model
|
||||
return $this->hasMany(LinkClick::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shareable short URL for this slug — always derived at runtime from the
|
||||
* owner's default domain (custom or platform), never stored.
|
||||
*/
|
||||
public function publicUrl(?User $owner = null): string
|
||||
{
|
||||
$owner ??= $this->user;
|
||||
@@ -52,11 +57,74 @@ class ShortLink extends Model
|
||||
return rtrim($base, '/').'/'.$this->slug;
|
||||
}
|
||||
|
||||
public static function publicHost(): string
|
||||
{
|
||||
return strtolower((string) config('link.public_domain', 'ladl.link'));
|
||||
}
|
||||
|
||||
public static function publicBaseUrl(): string
|
||||
{
|
||||
$domain = (string) config('link.public_domain', 'ladl.link');
|
||||
return 'https://'.self::publicHost();
|
||||
}
|
||||
|
||||
return 'https://'.$domain;
|
||||
/**
|
||||
* Build the live redirect target: current destination_url plus any path
|
||||
* suffix and query string from the inbound request (query keys on the
|
||||
* short link override destination keys when both are set).
|
||||
*/
|
||||
public function resolveRedirectUrl(Request $request, ?string $path = null): string
|
||||
{
|
||||
$base = trim((string) $this->destination_url);
|
||||
if ($base === '') {
|
||||
return $this->publicUrl();
|
||||
}
|
||||
|
||||
$suffix = $path !== null ? trim($path, '/') : '';
|
||||
if ($suffix !== '') {
|
||||
$parsed = parse_url($base);
|
||||
$basePath = rtrim((string) ($parsed['path'] ?? ''), '/');
|
||||
$rebuilt = ($parsed['scheme'] ?? 'https').'://'.($parsed['host'] ?? '');
|
||||
if (! empty($parsed['port'])) {
|
||||
$rebuilt .= ':'.$parsed['port'];
|
||||
}
|
||||
$rebuilt .= $basePath.'/'.$suffix;
|
||||
if (! empty($parsed['query'])) {
|
||||
$rebuilt .= '?'.$parsed['query'];
|
||||
}
|
||||
if (! empty($parsed['fragment'])) {
|
||||
$rebuilt .= '#'.$parsed['fragment'];
|
||||
}
|
||||
$base = $rebuilt;
|
||||
}
|
||||
|
||||
$requestQuery = $request->query();
|
||||
if ($requestQuery === []) {
|
||||
return $base;
|
||||
}
|
||||
|
||||
$parts = parse_url($base);
|
||||
$existing = [];
|
||||
if (! empty($parts['query'])) {
|
||||
parse_str((string) $parts['query'], $existing);
|
||||
}
|
||||
|
||||
// Inbound short-link params win so campaigns can override destination defaults.
|
||||
$merged = array_merge($existing, $requestQuery);
|
||||
$query = http_build_query($merged);
|
||||
|
||||
$target = ($parts['scheme'] ?? 'https').'://'.($parts['host'] ?? '');
|
||||
if (! empty($parts['port'])) {
|
||||
$target .= ':'.$parts['port'];
|
||||
}
|
||||
$target .= $parts['path'] ?? '';
|
||||
if ($query !== '') {
|
||||
$target .= '?'.$query;
|
||||
}
|
||||
if (! empty($parts['fragment'])) {
|
||||
$target .= '#'.$parts['fragment'];
|
||||
}
|
||||
|
||||
return $target;
|
||||
}
|
||||
|
||||
public function isExpired(): bool
|
||||
|
||||
Reference in New Issue
Block a user