Laravel Bitly

shivella/laravel-bitly image

Laravel Bitly stats

Downloads
455.4K
Stars
73
Open Issues
0
Forks
29

View on GitHub →

Laravel package for generating bitly url

Laravel Bitly Package

A laravel package for generating Bitly short URLs.

For more information see Bitly

Requirements

Laravel 5.1 or later

Installation

Installation is a quick 3 step process:

  1. Download laravel-bitly using composer
  2. Enable the package in app.php
  3. Configure your Bitly credentials
  4. (Optional) Configure the package facade

Step 1: Download laravel-bitly using composer

Add shivella/laravel-bitly by running the command:

composer require shivella/laravel-bitly

Step 2: Enable the package in app.php

Register the Service in: config/app.php

Shivella\Bitly\BitlyServiceProvider::class,

Step 3: Configure Bitly credentials

php artisan vendor:publish --provider="Shivella\Bitly\BitlyServiceProvider"

Add this in you .env file

BITLY_ACCESS_TOKEN=your_secret_bitly_access_token

Step 4 (Optional): Configure the package facade

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,
],
// ...
];

Usage

<?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

Testing

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.

Shivella photo

Webdeveloper using PHP, Symfony, React and Redux

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Shivella Laravel Bitly Related Articles

Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift