PHP code example of jeffersongoncalves / laravel-short-url
1. Go to this page and download the library: Download jeffersongoncalves/laravel-short-url 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/ */
jeffersongoncalves / laravel-short-url example snippets
use JeffersonGoncalves\LaravelShortUrl\Facades\ShortUrl;
// Create
$link = ShortUrl::create(['destination_url' => 'https://example.com/product']);
// Fluent
$link = ShortUrl::destination('https://example.com/product')
->key('promo25')
->expiresAt(now()->addDays(30))
->maxVisits(1000)
->password('secret')
->create();
// Resolve
$link = ShortUrl::resolve('promo25');
// The ready-to-share URL — custom domain when set, otherwise the app's own host
$link->fullUrl(); // https://short.test/promo25
use JeffersonGoncalves\LaravelShortUrl\Models\UtmTemplate;
$campaign = UtmTemplate::create(['name' => 'Spring SMS', 'utm_medium' => 'sms', 'utm_campaign' => 'spring-sale']);
$link = ShortUrl::destination('https://example.com/product')
->utmTemplate($campaign->id) // fills in unset utm_* fields from the template
->utm(['utm_source' => 'agent-42']) // explicit values always win over the template
->create();
// App\Providers\AppServiceProvider
use JeffersonGoncalves\LaravelShortUrl\Contracts\TenantResolver;
public function register(): void
{
$this->app->bind(TenantResolver::class, function () {
return new class implements TenantResolver
{
public function resolve(): int|string|null
{
return \App\Models\Tenant::current()?->id;
}
};
});
}
// App\Providers\AppServiceProvider
use JeffersonGoncalves\LaravelShortUrl\Contracts\CustomDomainResolver;
use JeffersonGoncalves\LaravelShortUrl\Models\CustomDomain;
public function register(): void
{
$this->app->bind(CustomDomainResolver::class, function () {
return new class implements CustomDomainResolver
{
public function resolve(string $host): ?CustomDomain
{
$account = \App\Models\Account::whereDomain($host)->first();
if (! $account) {
return null;
}
return (new CustomDomain)->forceFill([
'id' => $account->id,
'tenant_id' => $account->id,
'is_verified' => true,
]);
}
};
});
}