In this article
Introduction
I've just released Short URL v8.1.0 which includes a cool new feature that was contributed by Lewis Smith.
So I thought I'd write a quick post to highlight the new feature and how you can use it in your own Laravel projects.
If you're unfamiliar with Short URL, you can check out my "How to Create Short URLs in Laravel" article which talks about it in detail and shows you how to use it.
Specifying Allowed URL Schemes
Previously, Short URL only allowed you to create shortened URLs for URLs that began with http://
or https://
.
For example, you could create a shortened URL for https://ashallendesign.co.uk
like so:
1use AshAllenDesign\ShortURL\Facades\ShortURL;2 3$shortURL = ShortURL::destinationUrl('https://ashallendesign.co.uk')4 ->make()5 ->default_short_url;6 7// $shortURL is equal to: https://yourapp.com/short/abc123
However, you wouldn't be able to make a short URL like for a mailto://
URL like so:
1use AshAllenDesign\ShortURL\Facades\ShortURL;2 3$shortURL = ShortURL::destinationUrl('mailto://mail@ashallendesign.co.uk')4 ->make();
Running the above code would result in an AshAllenDesign\ShortURL\Exceptions\ShortURLException
being thrown with the following message:
1The destination URL must begin with http:// or https://
Although this restriction is useful in most cases, there are times when you may want to allow URLs to be created using other schemes. For example, you might want to use the mailto://
scheme to create a short URL that opens the user's email client with a pre-filled email address and subject. Or, maybe you want to use an application's custom scheme so a URL can open your app on a user's device (for example, phpstorm://
to open PHPStorm, or whatsapp://
to open WhatsApp).
Lewis Smith contributed a new feature (added in PRs #287 and #288) that allows you to explicitly specify the URL schemes that are allowed when creating a short URL.
You can do this by setting the allowed_url_schemes
field in your short-url.php
config file. By default, the allowed_url_schemes
field is set to allow http://
and https://
URLs like so:
config/short-url.php
1return [23 // ...45 'allowed_url_schemes' => [6 'http://',7 'https://',8 ],910 // ...1112];
Let's say you want to allow the mailto://
scheme as well. You can do this by adding it to the allowed_url_schemes
array like so:
config/short-url.php
1return [23 // ...45 'allowed_url_schemes' => [6 'http://',7 'https://',8 'mailto://',9 ],1011 // ...1213];
Now, you can create a short URL for a mailto://
URL like so:
1use AshAllenDesign\ShortURL\Facades\ShortURL;2 3$shortURL = ShortURL::destinationUrl('mailto://mail@ashallendesign.co.uk')4 ->make();
The above code will now successfully create a new short URL. Pretty cool, right!?
Like before, if you attempt to use a URL scheme that isn't allowed, an AshAllenDesign\ShortURL\Exceptions\ShortURLException
will be thrown with the following message:
1The destination URL must begin with an allowed prefix: http://, https://, mailto://
Conclusion
Thanks to Lewis for contributing this new feature to Short URL. It's a great addition that allows you to create short URLs for a wider range of URL schemes, while still being able to restrict the schemes used.