Installation:
composer require c-delouvencourt/short-url
php artisan vendor:publish --provider="Cdelouvencourt\ShortUrl\ShortUrlServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Cdelouvencourt\ShortUrl\ShortUrlServiceProvider" --tag="config"
php artisan migrate
First Use Case: Generate a short URL from a long URL:
use Cdelouvencourt\ShortUrl\Facades\ShortUrl;
$shortUrl = ShortUrl::create('https://example.com/very-long-url');
echo $shortUrl->short; // Outputs: e.g., "your-app.com/abc123"
Where to Look First:
config/short-url.php for configuration options (e.g., custom domains, key length).app/Models/ShortUrl.php (if published) to understand the Eloquent model structure.routes/web.php for default route bindings (e.g., /{shortKey}).Generating Short URLs:
$shortUrl = ShortUrl::create('https://example.com/long-url');
$shortUrl = ShortUrl::create('https://example.com/long-url', 'MYCUSTOMKEY');
$shortUrl = ShortUrl::create('https://example.com/long-url', null, now()->addDays(7));
Retrieving Short URLs:
$shortUrl = ShortUrl::findByShortKey('abc123');
Route::get('/{shortKey}', function ($shortKey) {
return redirect()->to(ShortUrl::findByShortKey($shortKey)->longUrl);
});
Tracking Visits:
config/short-url.php:
'track_visits' => true,
$shortUrl->logVisit(request()->ip());
Bulk Operations:
$urls = ['url1', 'url2', 'url3'];
$shortUrls = ShortUrl::createMultiple($urls);
Custom Domains:
config/short-url.php:
'domain' => 'short.yourdomain.com',
$absoluteUrl = ShortUrl::getAbsoluteUrl($shortUrl);
API Integration:
Route::post('/short-url', function (Request $request) {
return ShortUrl::create($request->longUrl);
});
return response()->json(['short_url' => $shortUrl->short]);
Queueing URL Creation:
use Cdelouvencourt\ShortUrl\Jobs\CreateShortUrl;
CreateShortUrl::dispatch('https://example.com/long-url');
Middleware for Tracking:
namespace App\Http\Middleware;
use Closure;
use Cdelouvencourt\ShortUrl\Facades\ShortUrl;
class TrackShortUrlVisits
{
public function handle($request, Closure $next)
{
if ($shortKey = $request->route('shortKey')) {
$shortUrl = ShortUrl::findByShortKey($shortKey);
$shortUrl->logVisit($request->ip());
}
return $next($request);
}
}
app/Http/Kernel.php:
protected $middleware = [
// ...
\App\Http\Middleware\TrackShortUrlVisits::class,
];
Seeding Short URLs:
use Cdelouvencourt\ShortUrl\Models\ShortUrl;
use Illuminate\Database\Seeder;
class ShortUrlSeeder extends Seeder
{
public function run()
{
ShortUrl::create(['long_url' => 'https://example.com']);
}
}
Customize the Model:
ShortUrl model to add custom fields:
namespace App\Models;
use Cdelouvencourt\ShortUrl\Models\ShortUrl as BaseShortUrl;
class ShortUrl extends BaseShortUrl
{
protected $casts = [
'custom_field' => 'string',
];
}
// In AppServiceProvider's boot method
ShortUrl::setModel(\App\Models\ShortUrl::class);
Custom Key Generation:
use Cdelouvencourt\ShortUrl\Contracts\ShortUrlGenerator;
class CustomShortUrlGenerator implements ShortUrlGenerator
{
public function generate()
{
return Str::random(8); // Custom logic
}
}
AppServiceProvider:
$this->app->bind(ShortUrlGenerator::class, CustomShortUrlGenerator::class);
Validation:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make(['longUrl' => $request->longUrl], [
'longUrl' => 'required|url|max:2048',
]);
Caching:
$shortUrl = Cache::remember("short_url_{$longUrl}", now()->addHours(1), function () use ($longUrl) {
return ShortUrl::create($longUrl);
});
Key Collisions:
$shortUrl = ShortUrl::create('https://example.com', 'UNIQUE_KEY');
Tracking Overhead:
'track_visits' => env('SHORT_URL_TRACK_VISITS', false),
Custom Domain Misconfiguration:
domain in config/short-url.php will break absolute URL generation.'domain' => env('SHORT_URL_DOMAIN', 'short.yourdomain.com'),
Migration Conflicts:
short_urls table manually, migrations may fail.php artisan vendor:publish --provider="Cdelouvencourt\ShortUrl\ShortUrlServiceProvider" --tag="migrations"
Rate Limiting:
Route::middleware(['throttle:60,1'])->get('/{shortKey}', function ($shortKey) {
// Redirect logic
});
Short URL Not Found:
$shortUrl = ShortUrl::where('short_key', $shortKey)->first();
routes/web.php:
Route::get('/{shortKey}', [ShortUrlController::class, 'redirect'])->where('shortKey', '.*');
Redirect Loop:
short field in the database matches the route parameter.Tracking Not Working:
track_visits is enabled in the config.Kernel.php.Custom Model Not Loading:
AppServiceProvider:
ShortUrl::setModel(\App\Models\ShortUrl::class);
'domain' => env('SHORT_URL
How can I help you explore Laravel packages today?