I recently tagged and released a new major version (v7.0.0) of one of my packages, Short URL. So, I thought I'd put together a Quickfire post to give a quick overview of what has been added and changed.
What is Short URL?
If you don't already know, Short URL is an open-source Laravel package that you can use to create short URLs for your applications. It comes with different options for tracking users that click your short URL and it only takes a couple of minutes to add it to your Laravel project.
To get a better idea of what the package does, let's take a quick look at some example code. Imagine that you have a Laravel app hosted on
https://example.com and you want to create a short URL to redirect the user to
https://mangotwo.com. To do this, your code would look something like this:
1use AshAllenDesign\ShortURL\Facades\ShortURL;23$shortUrl = ShortURL::destinationUrl('https://mangotwo.com')->make();
We can then imagine that this code would create a short URL similar to this:
http://example.com/short/abc123. Now, if you were to navigate to this URL, you'd be redirected to
https://mangotwo.com and your visit would be recorded in the database.
As a side note, if you haven't been reading my Round Ups (my last round one was Round Up: March 2022), you might not know that I'm actually building a small privacy-first, open source URL shortener service using this package. The service is called Mango Two and is still slowly being built when I get time to spend on it. There's already an early version of a Chrome extension available for installing that let's you start using Mango Two in under 30 seconds! If you get chance to check out Mango Two, or the Chrome extension, any feedback and contributions are more than welcome!
Create Short URLs Without Prefixes
As part of v7.0.0, you can now choose to remove the prefix completely from your short URLs.
Taking our example from above, let's say that we want to create a short URL on our Laravel app at
https://example.com and redirect the user to
https://mangotwo.com. By default, the
prefix field in the package's
short-url.php config file sets the prefix to "short". So, if we created a short URL, it would look something like this
But, let's say that you want to completely remove the prefix. To do this, you can simply publish the package's config and then update your
short-url.prefix config field to
null like so:
1return [23 // ...45 'prefix' => null,67 // ...89];
Now, if we were to create a short URL, it would look something like this
Define Middleware for Short URLs
Another new feature was added to v7.0.0 by Hafiq (@afiqiqmal) that adds the ability to define middleware for short URLs.
Prior to this version, the default short URL route that ships with the package didn't use any middleware. To run your short URLs through middleware, you'd need to define your own custom route.
However, with this new feature, you define the middleware that should be used via the
middleware field in the
short-url.php config file. For example, if we had an
AccountIsActive middleware that we wanted to run, we could update our config like so:
1return [23 // ...45 'middleware' => [6 \App\Middleware\AccountIsActive::class,7 ],89 // ...1011];
Similarly, we can also use the config field to define middleware groups that the route should use rather than just individual middleware classes. For example, if we wanted to run the short URLs through the
web middleware group, we could update our config like so:
1return [23 // ...45 'middleware' => [6 'web',7 ],89 // ...1011];
Set the Key Generator On-the-fly
Thanks to another cool addition by Victor-Emil Rossil Andersen (@Victor-emil), as of v7.0.0, we can now change the
KeyGenerator class on-the-fly. By allowing this, it gives you the ability to change the alphabet, salt, and key length when generating multiple short URLs in the same request.
For example, if you wanted to change the key generator being used in your code, you could change it using the facade approach like so:
1ShortUrl::destinationUrl('https://google.com')2 ->keyGenerator(new KeyGenerator(new Hashids('salt', 5, 'abcdef')))3 ->make();
Or, if you're creating your short URLs directly using the
Builder class, you can change it like so:
1$builder = new Builder(2 null,3 new KeyGenerator(new Hashids('salt', 5, 'abcdef'))4);56$builder->destinationUrl('https://google.com')->make();
Hopefully, this post should have shown you the new changes that have been made to Short URL v7.0.0.
If you enjoyed reading this post, I'd love to hear about it. Likewise, if you have any feedback to improve the future ones, I'd also love to hear that too.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀