Shortening
Basic usage
Statically:
use BradieTilley\Shortify\Shortify;
$short = Shortify::url($longUrl);
$short->url; // https://app.test/s/Ws4BYCVLDDDh
$short = Shortify::make()->shorten($longUrl);
Dependency injection:
use BradieTilley\Shortify\Shortify;
public function handle(Shortify $shortify): void
{
$url = $shortify->shorten($this->invoice->getSignedUrl());
echo $url->url;
}
Custom codes
Shortify::url($longUrl, code: 'ABC123');
If the code already exists (including soft-deleted rows), a
ShortifyUrlCodeAlreadyExistsException is thrown.
Auto-generated codes use the bound CodeGenerator (default: Str::random with
routing.code_length). Generation retries on collision and fails with
ShortifyCodeGenerationFailedException after 1000 attempts.
Expiry
$short = Shortify::url($longUrl, expiry: now()->addDay());
Visiting an expired URL throws ShortifyExpiredException and dispatches
ExpiredUrlAttempted.
Extra attributes
Pass additional Eloquent attributes when shortening:
Shortify::make()->shorten($longUrl, attributes: [
'user_id' => $user->id,
]);
When authenticated, user_id is set automatically unless you provide it
explicitly. See Ownership.