adspray/short-url-bundle
Symfony bundle providing a URL shortener service and Twig filter. Generate short paths like /~ShE from long URLs in controllers or templates, and resolve short codes back to the original URL via routing and a simple shortener service.
Installation:
vendors install or Git submodule as per README.Bumz\ShortUrlBundle\BumzShortUrlBundle in AppKernel.php.app/config/routing.yml:
BumzShortUrlBundle:
resource: "@BumzShortUrlBundle/Resources/config/routing.yml"
prefix: /
First Use Case:
app_dev.php/users/show/123 → your.host/~users123).short_url_generator service in a controller:
use Bumz\ShortUrlBundle\Service\ShortUrlGenerator;
class UsersController extends Controller {
public function showAction($id) {
$generator = $this->get('short_url_generator');
$shortUrl = $generator->generate('users_show', ['id' => $id]);
// Returns: "your.host/~users123"
}
}
Twig Integration:
short_url Twig filter in templates:
<a href="{{ path('users_show', {'id': 123})|short_url }}">Short Link</a>
Route-Based Shortening:
users_show → ~users{id}).$generator->generate('product_detail', ['slug' => 'laptop']);
// Output: "your.host/~productlaptop"
Dynamic Shortening:
blog_post + id=42 → ~blog42).Twig Filters:
short_url filter to any route path:
{{ path('homepage')|short_url }} {# "your.host/~home" #}
Custom Shortening Logic:
ShortUrlGenerator service to add custom rules (e.g., exclude certain routes):
$generator->setExcludedRoutes(['admin_dashboard']);
<form action="{{ path('user_edit', {'id': user.id})|short_url }}">
return $this->json(['short_url' => $generator->generate('api_download', ['id' => 1])]);
$cache = $this->get('cache');
$shortUrl = $cache->get('short_url_' . $routeName) ?: $generator->generate($routeName);
Route Name Collisions:
home and homepage), the generator will produce ambiguous short URLs.Parameter Sanitization:
id=1&action=delete → ~users1actiondelete).$generator->setParameterSanitizer(function($params) {
return array_map('urlencode', $params);
});
Routing Conflicts:
~ prefix might conflict with existing routes (e.g., your.host/~admin).routing.yml or adjust the bundle’s routing logic.Case Sensitivity:
~UserShow ≠ ~usershow).404 Errors: If a short URL returns a 404, verify:
~ prefix is correctly mapped in the bundle’s routing file.Logging: Enable debug mode to trace short URL generation:
$generator->setDebug(true); // Logs generated URLs to Symfony’s logger.
Custom Short URL Format:
Override the ShortUrlGenerator to change the format (e.g., your.host/s/{hash}):
class CustomShortUrlGenerator extends \Bumz\ShortUrlBundle\Service\ShortUrlGenerator {
protected function generateShortCode($routeName, $params) {
return 's/' . md5($routeName . serialize($params));
}
}
Register it as a service in services.yml:
services:
short_url_generator:
class: AppBundle\Service\CustomShortUrlGenerator
arguments: [...]
Database Backing: Extend the bundle to store short URLs in a database for persistence:
$generator->setStorage(new DoctrineShortUrlStorage($entityManager));
Analytics: Add tracking to short URLs by wrapping the generator:
$generator->setUrlWrapper(function($shortUrl) {
return $shortUrl . '?utm_source=short_url';
});
Prefix Customization:
The ~ prefix is hardcoded in the routing file. To change it:
Resources/config/routing.yml in your project._path pattern (e.g., /{shortCode} → /s/{shortCode}).Parameter Handling:
The bundle concatenates parameters directly. For complex cases (e.g., arrays), implement a custom ShortUrlGenerator:
protected function generateShortCode($routeName, $params) {
return $routeName . '_' . md5(serialize($params));
}
How can I help you explore Laravel packages today?