DEV Community

Cover image for Shrinkr: Your Ultimate Laravel URL Shortener
Nasrul Hazim Bin Mohamad
Nasrul Hazim Bin Mohamad

Posted on

Shrinkr: Your Ultimate Laravel URL Shortener

Managing and sharing long, cluttered URLs can be a hassle—this is where Shrinkr steps in. Shrinkr is an open-source Laravel package for shortening URLs, providing a simple yet powerful way to create short links with added functionality like click tracking, logging, analytics, and future support for branded domains, password protection, and QR codes. Whether you’re building a custom SaaS or just need a tool for personal projects, Shrinkr offers a solid foundation for your URL management needs.


Why Shrinkr?

Shrinkr goes beyond just shortening URLs. It offers built-in analytics to monitor link performance and flexible logging mechanisms for tracking user activity. With an easy-to-use API, Shrinkr ensures that developers can integrate it seamlessly into their existing Laravel applications.

GitHub Repository: Shrinkr on GitHub

Feature Discussions: Shrinkr Feature Ideas


Core Features

  • Shorten URLs: Create short links with or without custom slugs.
  • Click Analytics: Track visitor IPs, referrers, user agents, and devices.
  • Flexible Logging: Log data to files or store logs in the database.
  • Configurable API Support (Planned): Programmatically create and manage URLs.
  • Branded Domains (Planned): Use custom domains to brand your links.
  • Password-Protected URLs (Planned): Add a layer of security by restricting access.
  • QR Codes (Planned): Generate and share QR codes for easier access on mobile devices.
  • Link Health Monitoring (Planned): Ensure your links remain valid and notify users if they break.
  • Event & Listeners (Planned): Decouple logic and handle tasks asynchronously with Laravel’s event system.

Getting Started with Shrinkr

Installation

Install the package via Composer:

composer require cleaniquecoders/shrinkr
Enter fullscreen mode Exit fullscreen mode

Publish and run the migrations:

php artisan vendor:publish --tag="shrinkr-migrations"
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Publish the config file:

php artisan vendor:publish --tag="shrinkr-config"
Enter fullscreen mode Exit fullscreen mode

You can now customize your settings in the config/shrinkr.php file.


How to Use Shrinkr

Shrinkr is easy to integrate into your Laravel application with the provided facade.

use CleaniqueCoders\Shrinkr\Facades\Shrinkr;

// Shorten a URL
$shortUrl = Shrinkr::shorten('https://example.com/long-url', auth()->id());
echo $shortUrl; // Outputs: https://yourdomain.com/abc123

// Retrieve the original URL
$originalUrl = Shrinkr::resolve('abc123');
echo $originalUrl; // Outputs: https://example.com/long-url
Enter fullscreen mode Exit fullscreen mode

Logging and Analytics

Shrinkr captures detailed analytics during every redirect, including IP addresses, browsers, platforms, referrers, and query parameters. This data is stored in the redirect_logs table or can be logged to a file, depending on your configuration.

Example database log entry:

url_id ip browser platform referrer created_at
1 192.168.1.1 Chrome Windows google.com 2024-10-18 12:34:56

Configuring Logging

You can switch between logging to a file or database by changing the logger in config/shrinkr.php:

'logger' => \CleaniqueCoders\Shrinkr\Actions\Logger\LogToFile::class, // Default
Enter fullscreen mode Exit fullscreen mode

Or use database logging:

'logger' => \CleaniqueCoders\Shrinkr\Actions\Logger\LogToDatabase::class,
Enter fullscreen mode Exit fullscreen mode

Future Roadmap: What’s Coming Next?

We have several exciting features planned to enhance Shrinkr:

  1. Rate Limiting: Prevent abuse by limiting requests to the API.
  2. Protected URLs: Add password protection to sensitive links.
  3. QR Code Support: Generate QR codes for mobile sharing.
  4. Link Health Monitoring: Automatically detect and notify users of broken URLs.
  5. Events & Listeners: Handle tasks asynchronously, such as sending notifications when a link is accessed.

For more discussions on upcoming features, visit the Shrinkr GitHub Discussions.


Conclusion: Build Smarter with Shrinkr

Shrinkr isn’t just a URL shortener—it’s a powerful tool that helps you manage and analyze your links with precision. Whether you need basic URL shortening, advanced analytics, or custom branded links, Shrinkr has got you covered.

Explore the project on GitHub: Shrinkr Repository.

Join the conversation: Future Feature Discussions.

Get started today and take control of your links with Shrinkr! 🚀


Photo by Campaign Creators on Unsplash

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

Full disclosure: I'm not a fan of link shorteners for any purpose.

That over with, I'm curious about the password protection you mention. If the password protection prevents the end user from being redirected to the destination URL, but the destination itself isn't password-protected, then isn't this security by obscurity but with extra steps?

The "Link Health Monitoring" feature is a good idea, but since you're adding an intermediary, you're adding another point of failure anyway. If your shortener glitches or goes away, people who only have the short version saved can't get to the real URL.

Analytics will also depend on how the redirect works. If it's a permanent redirect, then you won't be able to track repeat visitors. Even if it's a temporary redirect, then whether a hit gets registered depends whether the repeat visitor returns from a bookmark or by using their browser history. It's these extra layers of "maybe" that make analytics end up looking like the Drake equation!