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();

$link->qrCode()->svg();                                // string
$link->qrCode()->png();                                // string
$link->qrCode(size: 400, margin: 20)->dataUri();        // data:image/png;base64,...
$link->qrCode()->dataUri('svg');                        // data:image/svg+xml;base64,...

// Weighted A/B rotation
ShortUrl::create([
    'destination_url' => 'https://example.com/base', // fallback
    'destination_type' => 'split',
    'rotation_variants' => [
        ['url' => 'https://a.test', 'weight' => 50, 'label' => 'A'],
        ['url' => 'https://b.test', 'weight' => 50, 'label' => 'B'],
    ],
]);

// Conditional targeting, evaluated per request
ShortUrl::create([
    'destination_url' => 'https://example.com/base', // used when no rule matches
    'destination_type' => 'rules',
    'targeting_rules' => [
        [
            'conditions' => [
                ['type' => 'country', 'value' => 'FR'],
                ['type' => 'device', 'value' => 'mobile'],
            ],
            'destination' => 'https://example.com/france-mobile',
        ],
    ],
]);

// 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;
            }
        };
    });
}

// config/short-url.php
'tenancy' => ['enabled' => true],

// 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,
                ]);
            }
        };
    });
}

ShortUrl::create(array $attributes): ShortUrlModel
ShortUrl::destination(string $url): ShortUrlBuilder
ShortUrl::resolve(string $key, ?string $host = null): ?ShortUrlModel

// ShortUrlModel
$shortUrl->fullUrl(): string // ready-to-share link (custom domain or app host)
$shortUrl->qrCode(int $size = 300, int $margin = 10): QrCodeGenerator // ->svg() / ->png() / ->dataUri()

// ShortUrlBuilder, in addition to the setters shown above
->customDomain(?int $customDomainId)
->utmTemplate(int $utmTemplateId)
->utm(array $attributes) // utm_source, utm_medium, utm_campaign, utm_term, utm_content

// src/Contracts/
VisitRepository, GeoIpDriver, VpnDetectionDriver, AnalyticsDriver,
SafeBrowsingChecker, StatsAggregator, TargetingResolver,
DnsVerifier, SettingsRepository, ImporterDriver,
ConversionApiDispatcher, TenantResolver, PlanResolver, CustomDomainResolver

// src/Registries/
FilterTypeRegistry, AnalyticsDriverRegistry,
PixelProviderRegistry, ImporterDriverRegistry
bash
php artisan vendor:publish --tag="short-url-config"
php artisan vendor:publish --tag="short-url-migrations"
php artisan vendor:publish --tag="short-url-translations"
php artisan migrate