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