Introduction
Earlier this week, I tagged a v5.2.0 release for my Short URL package (ashallendesign/short-url). It had some helpful additions by Victor-Emil Rossil Andersen, one of which was being able to set a custom alphabet that can be used when automatically generating a short URL.
In this Quickfire post, we're going take a look at how you can use this new change when generating short URLs in your own Laravel application.
Setting a Custom Alphabet
By default, whenever you build a short URL using the package, the URL key is automatically generated for you using the Hashids package.
In the past, the keys that were generated could contain any numbers, lowercase letters, and uppercase letters. However, as of v5.2.0, you can now set explicitly define the letters and numbers that the package should use by changing the alphabet
field in the short-url.php
config file.
If you're installing v5.2.0 or later in your project, the field will already be in your config file if you choose to publish the config file using the following command:
1php artisan vendor:publish --provider="AshAllenDesign\ShortURL\Providers\ShortURLProvider"
However, if you already had an earlier version of Short URL installed in your project, you'll need to add the new field to your own config/short-url.php
like so:
1return [ 2 3 // ... 4 5 /* 6 |-------------------------------------------------------------------------- 7 | Alphabet 8 |-------------------------------------------------------------------------- 9 |10 | Define the characters allowed in the output short URL keys.11 | The 'alphabet' must be at least 16 unique characters12 | and cannot contain spaces.13 |14 */15 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',16 17 // ...18 19];
Now, if you want to only allow certain letters or numbers to be used, you'll just need update the alphabet
field. As mentioned in the config's comments, you'll just need to make sure that there are at least 16 unique characters and that there aren't any spaces.
For example, let's imagine that we want to only allow uppercase characters to be used when generating the keys. To do this, we'd just need to update the config option like so:
1return [ 2 3 // ... 4 5 /* 6 |-------------------------------------------------------------------------- 7 | Alphabet 8 |-------------------------------------------------------------------------- 9 |10 | Define the characters allowed in the output short URL keys.11 | The 'alphabet' must be at least 16 unique characters12 | and cannot contain spaces.13 |14 */15 'alphabet' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',16 17 // ...18 19];
As you can see, it's really easy to manually specify your own custom alphabet to be used.
If you're interested in finding out more about the Short URL package and using it in your Laravel project's, you can check it out in the GitHub repository.
Conclusion
Hopefully, this post should have given you a brief overview about how to specify your own alphabet in the Short URL package.
If this post helped you out, I'd love to hear about it. Likewise, if you have any feedback to improve this post, 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! 🚀