A laravel package for generating Bitly short URLs.
For more information see Bitly
Laravel 5.1 or later
Installation is a quick 3 step process:
- Download laravel-bitly using composer
- Enable the package in app.php
- Configure your Bitly credentials
- (Optional) Configure the package facade
Add shivella/laravel-bitly by running the command:
composer require shivella/laravel-bitly
Register the Service in: config/app.php
Shivella\Bitly\BitlyServiceProvider::class,
php artisan vendor:publish --provider="Shivella\Bitly\BitlyServiceProvider"
Add this in you .env file
BITLY_ACCESS_TOKEN=your_secret_bitly_access_token
Register the Bitly Facade in: config/app.php
<?php return [ 'aliases' => [ 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, // ... 'Bitly' => Shivella\Bitly\Facade\Bitly::class, ], // ... ];
<?php $url = app('bitly')->getUrl('https://www.google.com/'); // http://bit.ly/nHcn3
Or if you want to use facade, add this in your class after namespace declaration:
<?php use Bitly;
Then you can use it directly by calling Bitly::
like:
<?php $url = Bitly::getUrl('https://www.google.com/'); // http://bit.ly/nHcn3
In your unit tests you may use BitlyClientFake
class instead of regular client. It will create a fake short URLs using hashing without calling an external REST API, which will speed up your unit tests. Fake might be setup via DI at your \Tests\TestCase::createApplication()
implementation:
<?php namespace Tests; use Illuminate\Contracts\Console\Kernel; use Shivella\Bitly\Testing\BitlyClientFake; trait CreatesApplication { /** * Creates the application. * * @return \Illuminate\Foundation\Application */ public function createApplication() { $app = require __DIR__.'/../bootstrap/app.php'; $app->make(Kernel::class)->bootstrap(); // swap Bitly client by a fake $app->singleton('bitly', function () { return new BitlyClientFake(); }); return $app; } }
As an alternative you may use \Shivella\Bitly\Facade\Bitly::fake()
method to swap regular client by a fake.