LaraCrafts / laravel-url-shortener by yak0d3

Powerful URL shortening tools in Laravel
102,686
98
7
Package Data
Maintainer Username: yak0d3
Package Create Date: 2019-04-16
Package Last Update: 2020-12-02
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-08 03:19:53
Package Statistics
Total Downloads: 102,686
Monthly Downloads: 691
Daily Downloads: 31
Total Stars: 98
Total Watchers: 7
Total Forks: 40
Total Open Issues: 6

laravel-url-shortener

Powerful URL shortening tools in Laravel

Installation

You can easily install this package using Composer, by running the following command:

composer require laracrafts/laravel-url-shortener

Requirements

This package has the following requirements:

  • PHP 7.1 or higher
  • Laravel (or Lumen) 5.1 or higher

Laravel 5.5+

If you use Laravel 5.5 or higher, that's it. You can now use the package, continue to the usage section.

Laravel 5.1-5.4

If you're using an older version of Laravel, register the package's service provider to your application. You can do this by adding the following line to your config/app.php file:

'providers' => [
   ...
   LaraCrafts\UrlShortener\UrlShortenerServiceProvider::class,
   ...
],

Lumen

If you're using Lumen, register the package's service provider by adding the following line to your bootstrap/app.php file:

$app->register(LaraCrafts\UrlShortener\UrlShortenerServiceProvider::class);

Usage

The shortener can be retrieved from the container in two ways:

// This works in both Laravel and Lumen
$shortener = app('url.shortener');
// This only works in Laravel 5.2+
$shortener = url()->shortener();

Once you have an instance of the shortener, you can shorten your URLs:

// This will return your shortened URL as a string
$shortener->shorten(...);

// This will return a promise which will resolve to your shortened URL
$shortener->shortenAsync(...);

// You can also call shortening from Laravel's url component directly
app('url')->shorten(...);

// or...
url()->shorten(...);

// or even...
UrlShortener::shorten(...);

This package relies on Guzzle's promise library for its asynchronous shortening, read their documentation for more information.

You can also use dependency injection to inject the shortener into a method:

class MyController extends Controller
{
    public function myFunction(ShortenerManager $shortener)
    {
        $shortener->shorten(...);
    }
}

The shortener exposes the following methods:

Method | Description ---------------|------------------------------------- shorten | Shorten the given URL shortenAsync | Shorten the given URL asynchronously driver | Retrieve a driver (e.g. tiny_url) extend | Register your own driver

Changing the driver

You can change the default driver by setting URL_SHORTENER_DRIVER={driver} in your environment file or publishing the config file and changing it directly.

Adding your own drivers

Much like Laravels core components, you can add your own drivers for this package. You can do this by adding the following code to a central place in your application (preferably a service provider).

UrlShortener::extend('my_driver', function () {
    return new MyCustomUrlShortener();
});

If you are adding a web hosted shortener service you may want to extend the RemoteShortener abstract class, for which you can use the shipped drivers (e.g. TinyUrlShortener) as an example as to how.

If you wrote a custom driver that others might find useful (such as a public online shortener service), please consider adding it to the package via a pull request.

Available drivers

Below is a list of available drivers along with their individual specs:

Service | Driver name | Since version | Analytics | Monetization --------------------------------------------------|-------------|---------------|-----------|----------------- Bit.ly | bit_ly | 0.1.0 | yes | no Firebase Dynamic Links | firebase | 0.2.0 | yes | no Is.gd | is_gd | 0.2.0 | yes | no Ouo.io | ouo_io | 0.2.0 | yes | yes Shorte.st | shorte_st | 0.1.0 | yes | yes TinyURL | tiny_url | 0.1.0 | no | no V.gd | is_gd | 0.2.0 | yes | no

Bit.ly

website

This driver runs on Bit.ly's API and currently only supports API version 4. The API requires an access token and currently only generic access tokens are supported. You can retrieve such tokens from your Bit.ly profile. If you have a paid Bit.ly account you will also be able to set the domain for your shortened URLs.

Variable | Description -------------------|---------------------- BIT_LY_API_TOKEN | Your Bit.ly API token BIT_LY_DOMAIN | Your short URL domain

Firebase Dynamic Links

website

This driver runs on Firebase's API. The API requires an access token, a URI prefix and a suffix. You can access these information on you firebase console. The token accessible under the project settings as "Web API Key" and the prefixes can be defined and accessed under the Dynamic Links menu.

The suffix can have the value SHORT or UNGUESSABLE.

IMPORTANT! Links created via the API are not visible in the Firebase console. They are only accessible via the Analytics REST API.

Variable | Description | Default ----------------------|------------------------------------|--------------- FIREBASE_API_TOKEN | Your Firebase API token | FIREBASE_URI_PREFIX | Your URL prefix | FIREBASE_SUFFIX | The path component creation method | UNGUESSABLE

Is.gd / V.gd

website

This driver supports is.gd and v.gd trough their respective APIs. When link previews are enabled v.gd will be used, otherwise is.gd will be used.

Variable | Description ----------------------|---------------------------------------- IS_GD_LINK_PREVIEWS | Enable or disable destination previews IS_GD_STATISTICS | Enable or disable statistics

Ouo.io

website

This driver uses the Ouo.io API and requires an access token. The API allows for URL monetization via advertisements and provides analytics via its dashboard.

Variable | Description -------------------|---------------------- OUO_IO_API_TOKEN | Your Ouo.io API token

Shorte.st

website

This driver uses the Shorte.st API, which requires an access token. This API supports monetization of your URLs and can give you insight into your traffic via its dashboard.

Variable | Description ----------------------|------------------------- SHORTE_ST_API_TOKEN | Your Shorte.st API token

TinyURL

website

This driver runs on the TinyURL API, which requires no additional setup. This driver is the package default.