Sometimes, you need to shorten URL. Especially when you want to send URLs in SMS or Social Media. We will learn about laravel bitly URL shortener composer package.
Post Contents
# Requirements
- Laravel installed in your system. How to install laravel?
- Laravel bitly URL shortener package. Documentation.
# Getting Bitly URL Shortener Access Token
First of all, you need to get a bitly access token. Create a new free account into a bitly. Your dashboard will look like below.
Click on profile settings and after a click on generic access token.
Enter your account password and click on generic access token to get your bitly access token.
# Installing Bitly Package
Install laravel bitly package using following command
composer require shivella/laravel-bitly
Register the service into the app.php‘s providers array.
/* config > app.php */
'providers' => [
Shivella\Bitly\BitlyServiceProvider::class,
]
Similarly, register the facade into aliases array.
/* config > app.php */
'aliases' => [
'Bitly' => Shivella\Bitly\Facade\Bitly::class,
]
That’s It! You successfully configured laravel bitly.
# Usage
There are two methods you can use it.
$bitlyUrl = app('bitly')->getUrl('https://www.your-url.com/');
echo $bitlyUrl;
Likewise, you can use it with laravel facade.
/* declare bitly namespace */
use Bitly;
$bitlyUrl = Bitly::getUrl('https://www.google.com/');
echo $bitlyUrl;
Check the shortened URL to your browser. It will automatically redirect to your original URL.