PHP code example of shammaa / laravel-url-shortener

1. Go to this page and download the library: Download shammaa/laravel-url-shortener 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/ */

    

shammaa / laravel-url-shortener example snippets


use Shammaa\LaravelUrlShortener\Facades\UrlShortener;

// Create a simple short link
$link = UrlShortener::create([
    'destination_url' => 'https://example.com/very-long-url',
]);

echo $link->short_url; // https://yoursite.com/s/abc123

// Simple
$link = short_url('https://example.com');

// With options
$link = short_url('https://example.com', [
    'title' => 'My Link',
    'password' => 'secret123',
    'expires_in_days' => 7,
    'click_limit' => 50,
]);

$link = UrlShortener::create([
    'destination_url' => 'https://example.com/secret',
    'password' => 'my-password',
]);

$link = UrlShortener::create([
    'destination_url' => 'https://example.com',
]);

// Access QR code
$qrCodeUrl = $link->qr_code_path; // Storage path

// Or via API
GET /api/url-shortener/links/{key}/qr-code

$link = UrlShortener::create([
    'destination_url' => 'https://example.com',
    'utm_parameters' => [
        'utm_source' => 'newsletter',
        'utm_medium' => 'email',
        'utm_campaign' => 'promo2024',
    ],
    'utm_hidden' => true, // Hide in URL but track internally
]);

// Expires in 30 days
$link = UrlShortener::create([
    'destination_url' => 'https://example.com',
    'expires_in_days' => 30,
]);

// Or set specific date
$link = UrlShortener::create([
    'destination_url' => 'https://example.com',
    'expires_at' => now()->addMonths(3),
]);

$link = UrlShortener::create([
    'destination_url' => 'https://example.com',
    'click_limit' => 100, // Stops working after 100 clicks
]);

$link = UrlShortener::findByKey('abc123');

// Get statistics
$totalClicks = $link->clicks_count;
$uniqueClicks = $link->visits()->distinct('ip_address')->count();
$clicksToday = $link->visits()->today()->count();
$clicksByCountry = $link->visits()->selectRaw('country, COUNT(*) as clicks')
    ->groupBy('country')
    ->get();

use Shammaa\LaravelUrlShortener\Traits\HasShortLink;

class Post extends Model
{
    use HasShortLink;
    
    protected $fillable = ['title', 'slug', 'content'];
}

// Create short link
$post = Post::create(['title' => 'My Article']);
$post->createShortLink();

echo $post->short_url; // https://yoursite.com/s/post-xyz1
echo $post->shortLink->key; // post-xyz1

$post->createShortLink();      // Key: "post-xyz1"
$article->createShortLink();   // Key: "article-abc2"
$product->createShortLink();   // Key: "product-def3"

// Migration
Schema::table('posts', function (Blueprint $table) {
    $table->string('short_link_prefix')->nullable();
});

// Usage
$post->short_link_prefix = 'blog';
$post->createShortLink();
// Key: "blog-xyz1" (instead of "post-xyz1")

class Post extends Model
{
    use HasShortLink;
    
    protected function getShortLinkPrefix(): string
    {
        return 'blog'; // Always use "blog" as prefix
    }
}

class Post extends Model
{
    use HasShortLink;
    
    public function getShortLinkUrl(): string
    {
        return route('posts.show', $this);
    }
}

class Post extends Model
{
    use HasShortLink;
    
    protected static function booted()
    {
        static::created(function ($post) {
            $post->createShortLink(['title' => $post->title]);
        });
    }
}

// Model
class Post extends Model
{
    use HasShortLink;
    protected $fillable = ['title', 'slug', 'content'];
}

// Routes
Route::get('/posts/{post}', [PostController::class, 'show'])
    ->name('posts.show');

// Controller
public function show(Post $post)
{
    return view('posts.show', ['post' => $post]);
}

// Blade Template
@if($post->hasShortLink())
    <a href="{{ $post->short_url }}" target="_blank">
        Share: {{ $post->short_url }}
    </a>
@endif

// config/url-shortener.php
'model_key_length' => 4, // Default: 4 characters

// Or via .env
URL_SHORTENER_MODEL_KEY_LENGTH=6

// Short URL prefix
'prefix' => 's', // Links will be: /s/abc123

// Key length for manually created links
'key_length' => 6,

// Model key length (for HasShortLink trait)
'model_key_length' => 4,

// QR Code settings
'qr_code' => [
    'enabled' => true,
    'size' => 200,
    'format' => 'svg', // svg or png
],

// UTM tracking
'utm' => [
    'enabled' => true,
    'hidden' => true, // Hide in URL but track internally
],

// Tracking options
'track_visits' => true,
'track_ip_address' => true,
'track_user_agent' => true,
'track_referer' => true,
'track_geo' => false, // Requires geolocation service
bash
php artisan vendor:publish --tag=url-shortener-config
php artisan vendor:publish --tag=url-shortener-migrations
php artisan migrate