Installation:
vendors install or Git submodule (as per README).BumzShortUrlBundle in AppKernel.php.routing.yml:
BumzShortUrlBundle:
resource: "@BumzShortUrlBundle/Resources/config/routing.yml"
prefix: /
php app/console cache:clear
First Use Case:
$shortUrl = $this->get('short_url')->generate('user/profile/123');
{{ path('short_url', {'path': 'user/profile/123'}) }}
URL Generation:
$shortUrl = $this->get('short_url')->generate('/long/path');
{{ short_url('/long/path') }}
/user/123) or full URLs (e.g., https://example.com/user/123).Routing Integration:
~short to handle short URLs (e.g., http://your.host/~short/abc123).routing.yml if needed.Dynamic Short Codes:
$shortCode = $this->get('short_url')->generateShortCode('user', $userId);
<a href="{{ path('short_url', {'path': 'user/' ~ user.shortCode }) }}">View</a>
Twig Extensions:
short_url() and path('short_url', {...}) in templates for consistency.API/CLI Usage:
$shortUrl = $this->container->get('short_url')->generate('/api/resource');
Routing Conflicts:
~short doesn’t clash with existing routes. Override in routing.yml if needed:
short_url:
pattern: /~custom_prefix/{path}
defaults: { _controller: BumzShortUrlBundle:ShortUrl:redirect }
Short Code Collisions:
md5()). For high-traffic apps, implement a custom strategy (e.g., UUID or database-backed uniqueness) by extending the service:
class CustomShortUrlGenerator extends \Bumz\ShortUrlBundle\Service\ShortUrlGenerator
{
protected function generateShortCode($path) {
return Str::random(8); // Custom logic
}
}
services.yml:
services:
short_url:
class: AppBundle\Service\CustomShortUrlGenerator
arguments: [...]
HTAccess Requirements:
~short to the Symfony frontend controller. Example .htaccess rule:
RewriteRule ^~short/(.*)$ /app_dev.php/~short/$1 [L]
Caching Short URLs:
$cache = $this->get('cache');
$shortCode = $cache->get("short_url:{$path}");
if (!$shortCode) {
$shortCode = $this->get('short_url')->generate($path);
$cache->set("short_url:{$path}", $shortCode, 3600);
}
Check Route Generation:
short_url route exists:
php app/console router:debug | grep short_url
Log Short Codes:
$this->get('logger')->debug('Generated short code:', ['code' => $shortCode]);
Test Redirects:
http://your.host/~short/abc123) to ensure they redirect correctly.Custom Short Code Format:
ShortUrlGenerator class to enforce formats (e.g., alphanumeric-only):
protected function sanitizeShortCode($code) {
return preg_replace('/[^a-zA-Z0-9]/', '', $code);
}
Analytics Integration:
class CustomShortUrlController extends \Bumz\ShortUrlBundle\Controller\ShortUrlController
{
public function redirectAction($shortCode) {
$this->get('analytics')->track('short_url_click', ['code' => $shortCode]);
return parent::redirectAction($shortCode);
}
}
services.yml:
services:
short_url.controller:
class: AppBundle\Controller\CustomShortUrlController
arguments: [...]
Database-Backed Short Codes:
// Example: Add to ShortUrlGenerator
public function generate($path) {
$shortCode = $this->findOrCreateShortCode($path);
return $this->getBaseUrl() . '/~short/' . $shortCode;
}
protected function findOrCreateShortCode($path) {
$existing = $this->get('doctrine')->getRepository('AppBundle:ShortUrl')
->findOneBy(['path' => $path]);
if (!$existing) {
$existing = new ShortUrl();
$existing->path = $path;
$existing->code = $this->generateUniqueCode();
$this->get('doctrine')->getManager()->persist($existing);
}
return $existing->code;
}
How can I help you explore Laravel packages today?