Install via Composer (preferred method):
composer require alanmastro/short-url-bundle
(Note: The original README uses Git submodule, but Composer is cleaner for modern Laravel/Symfony integration.)
Register the Bundle (if using Symfony/Symfony-like structure):
Add to config/bundles.php:
return [
// ...
Bumz\ShortUrlBundle\BumzShortUrlBundle::class => ['all' => true],
];
Publish Config (if needed):
php artisan vendor:publish --tag=bumz-short-url-config
(Check config/short_url.php for customization like base path, hash length, or storage.)
First Use Case: Generate a short URL in a controller:
$shortUrl = app('bumz_short_url.shortener')->shorten('https://example.com/long-url');
// Returns: "/~abc123" (or your configured prefix)
Shortening URLs:
$shortener = app('bumz_short_url.shortener');
$shortUrl = $shortener->shorten('https://example.com');
<a href="{{ 'https://example.com' | shortenUrl }}">Short Link</a>
return response()->json(['short_url' => $shortUrl]);
Resolving Short URLs:
$longUrl = app('bumz_short_url.shortener')->getLong('abc123');
routing.yml).Custom Short Codes:
$shortCode = app('bumz_short_url.shortener')->generateShortCode();
$shortener->storeShortUrl($shortCode, 'https://example.com');
Laravel-Specific: Use the service container directly:
$shortener = resolve('bumz_short_url.shortener');
Or bind it in AppServiceProvider:
$this->app->bind('shortener', function() {
return app('bumz_short_url.shortener');
});
Middleware for Short URLs: Protect short URLs with middleware (e.g., rate-limiting):
Route::get('/~{short}', [ShortUrlController::class, 'redirect'])
->middleware('throttle:10,1');
Storage Backend: Extend the default storage (e.g., switch to Redis):
$shortener->setStorage(new RedisShortUrlStorage());
Custom Hashing: Override the default hash generator (e.g., for alphanumeric-only):
$shortener->setHashGenerator(new CustomHashGenerator());
Route Conflicts:
/~{short} route. Ensure no other routes conflict (e.g., Laravel’s default routes).prefix in routing.yml or use a custom route name.Case Sensitivity:
ABC ≠ abc).ShortUrlStorage:
$shortCode = strtolower($shortCode);
Collision Risk:
Caching:
$longUrl = Cache::remember("short_{$shortCode}", 3600, function() use ($shortCode) {
return app('bumz_short_url.shortener')->getLong($shortCode);
});
Laravel-Symfony Mismatch:
get() in favor of app() or dependency injection.$shortener = app('bumz_short_url.shortener');
Check Storage: Dump the storage backend to verify entries:
dd(app('bumz_short_url.shortener')->getStorage()->all());
Log Short Codes: Add logging for debugging collisions:
$shortener->setLogger(new SingleUseLogHandler(
Storage::disk('logs')->path('short_url.log')
));
Test Routes: Manually test short URLs:
curl http://your.app/~abc123
(Should return a 301/302 redirect to the long URL.)
Custom Storage:
Implement Bumz\ShortUrlBundle\Storage\ShortUrlStorageInterface:
class DatabaseShortUrlStorage implements ShortUrlStorageInterface {
// ...
}
Register it in services.yml:
bumz_short_url.shortener.storage: '@database_short_url_storage'
Custom Hash Generator:
Extend Bumz\ShortUrlBundle\Generator\ShortUrlGenerator:
class Base62Generator extends ShortUrlGenerator {
protected function generateHash() { /* ... */ }
}
Event Listeners:
Listen for short.url.created or short.url.resolved events to log/analyze usage.
Laravel Blade Directives: Add a Blade directive for short URLs:
Blade::directive('short', function($url) {
return "<?php echo app('bumz_short_url.shortener')->shorten($url); ?>";
});
Usage:
<a href="@short('https://example.com')">Link</a>
How can I help you explore Laravel packages today?