A flexible, extensible URL shortener library for PHP applications with multiple storage backends and event-driven architecture.
- 🚀 Multiple Storage Backends - Redis, File, or bring your own
- 📝 PSR-compliant Logging - Built-in Monolog integration
- 🎯 Event-Driven Architecture - Hook into URL processing lifecycle
- 🔧 Extensible Design - Easy to customize validators, code generators, and more
- 💪 Type-Safe - Full PHP type hints and exceptions
composer require nimp/link-loom<?php
use Nimp\LinkLoom\UrlShortenerBuilder;
use Redis;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$shortener = (new UrlShortenerBuilder())
->withRedisRepository($redis)
->build();
// Shorten URL
$code = $shortener->encode('https://example.com/long-url');
echo "Short code: " . $code;
// Expand URL
$url = $shortener->decode($code);
echo "Original URL: " . $url;$shortener = (new UrlShortenerBuilder())
->withRedisRepository($redis)
->withLogger('/path/to/urls.log')
->build();$shortener = (new UrlShortenerBuilder())
->withCustomRepository(new MySQLRepository())
->withCodeGenerator(new MyCustomCodeGenerator())
->withCustomValidator(new MyCustomValidator())
->build();$shortener = (new UrlShortenerBuilder())
->withRedisRepository($redis)
->withLogger('/path/to/urls.log')
->addListener(new AnalyticsListener())
->addListener(new CacheListener())
->build();$shortener = (new UrlShortenerBuilder())
->withRedisRepository($redis)
->withEventDispatcher($myCustomDispatcher)
->build();LinkLoom provides a comprehensive event system for monitoring the URL shortening lifecycle using the Observer pattern.
All events extend BaseShortenerEvent and provide access to the UrlShortener instance:
$event->context; // UrlShortener instance| Event | Trigger | Data |
|---|---|---|
EncodeStartEvent |
Before URL encoding starts | url, context |
EncodeSuccessEvent |
After successful URL encoding | url, code, context |
DecodeStartEvent |
Before code decoding starts | code , context |
DecodeSuccessEvent |
After successful code decoding | code, url, context |
ValidateErrorEvent |
When URL validation fails | url, message, context |
GetFromStorageErrorEvent |
When storage read fails | code, message, context |
SaveErrorEvent |
When storage save fails | message, context |
Method Description
encode(string $url): string Shorten URL and return code
decode(string $code): string Expand code to original URL