codeofdigital / laravel-url-shortener
Powerful URL shoterning tools using different drivers for your Laravel projects
Requires
- php: ^7.2|^8.0
- guzzlehttp/guzzle: ^6.2 || ^7.0
- guzzlehttp/promises: ^1.0
- guzzlehttp/psr7: ^1.4
- psr/http-message: ^1.0
Requires (Dev)
- illuminate/routing: ^5.1 || ^6.0 || ^7.0 || ^8.0
- phpunit/phpunit: ^8.2
This package is auto-updated.
Last update: 2024-10-29 06:12:43 UTC
README
Powerful URL shortening tool using different drivers for your Laravel projects
Table of Contents
Overview
A Laravel package that is used to shorten URLs according to your needs using your desired URL shortening drivers. Every driver will provide different features and function so do please check their documentation and pricing for different usages.
Installation
Requirements
The package has been developed to work with the following versions and minimum requirements:
- PHP 7.2 or higher
- Laravel 6.0 or higher
Install Pacakge
You can install the package via the latest Composer:
composer require codeofdigital/laravel-url-shortener
Publish Config
You can then publish the package's config file by using the following command:
php artisan vendor:publish --provider="CodeOfDigital\LaravelUrlShortener\UrlShortenerServiceProvider"
Usage
The quickest way to get started with creating a shortened URL is by using the snippet below.
The shorten()
method will return a shortened URL link, and you can freely use it within your system.
$shortUrl = new UrlShortener(); $shortUrl->shorten('https://example.com');
Instantiate Shortener
The URL Shortener can be retrieved from the container in few ways:
$shortener = app('url.shortener'); // or ... $shortener = url()->shortener();
This package also comes with the URL Shortener facade to instantiate the class:
use CodeOfDigital\LaravelUrlShortener\Facades\UrlShortener; $shortUrl = UrlShortener::shorten('https://example.com');
You can also use dependency injection to inject in one of your controller's method:
use CodeOfDigital\LaravelUrlShortener\UrlShortener; class MyController extends Controller { public function myFunction(UrlShortener $shortener) { $shortener->shorten('https://example.com'); } }
Once you have instantiate the URL Shortener class, you can use the methods and shorten your URLs:
// This will set and create the driver instance $shortener->driver('own-driver'); // This will return shortened URL in string $shortener->shorten('https://example.com'); // This will return a promise object which can be used to resolve and retrieve shortened URL later on $shortener->shortenAsync('https://example.com'); // Methods can be called from Laravel URL components url()->shorten('https://example.com'); // Or app('url.shortener')->shorten('https://example.com'); // Methods can be chained as well $shortener->driver('own-driver')->shorten('https://example.com');
The URL Shortener provides the following methods to use:
Changing Driver
You can change the default driver by setting URL_SHORTENER_DRIVER={driver}
in your environment file
or publish the config file and make your changes there directly.
Available Drivers
There are total of 8 drivers available in this package. By default, this package will use Bit.ly
as the main driver.
Below is a list of drivers with their respective specs:
Bit.ly
The main default driver for this package. It runs on Bit.ly API and currently using Version 4. To access the Bit.ly driver
in this package, you need to provide generic access token
for OAuth Authentication which can be retrieved in their Bit.ly
Link Management Dashboard. You can parse in your own custom short URL domain (Paid Version) as well.
Currently, there are 2 variables that needs to be set to use the driver.
TinyURL
Driver that runs on TinyURL API and currently using Version 2. To access the TinyURL driver in this package,
you need to provide the API token
for OAuth Authentication which can be retrieved in the TinyURL's account setting.
You can parse in your own custom short URL domain (Paid Version) as well. TinyURL provides 3 free custom URL domain prefix
which you can check out at their website. Currently, there are 2 variables that needs to be set to use the driver.
Shorte.st
Driver that runs on Shorte.st API and currently using Version 1. To access the Shorte.st driver in this package,
you need to provide an access token
which can be retrieved after you have register for a Shorte.st account.
This driver has monetization support where each link that has been clicked will earn a small amount of cash.
Currently, there are only 1 variable that needs to be set to use the driver.
Is.gd
Driver that runs on Is.gd API and currently using Version 1. No API token is needed for this API as there is no authentication required when sending API request. Currently, there are only 1 variable that needs to be set to use the driver. This variable determines whether statistic is enabled or disabled when shorten an URL link.
Cutt.ly
Driver that runs on Cutt.ly API and currently using Version 1. To access the Cutt.ly driver in this package,
you need to provide an API token
which can be retrieved in Cutt.ly dashboard. API token will be used within the query
parameters as a method of authentication. This driver comes with analytics and statistics.
Currently, there are only 1 variable that needs to be set to use the driver.
Hide.uri
Driver that runs on Hide.uri API and currently using Version 1. No API token is needed for this API as no authentication is required when sending API request. No variable is needed to setup the driver and you can use straight out of the box. Check the official website if there is any rate limiting on API calls.
Firebase Links
Driver that runs on Google Firebase API and currently using Version 1. To access the Firebase driver in this package,
you need to provide an access token
and a URI prefix which can be retrieved in your Firebase console Web API Token
.
The URI prefix can be set using your own custom domain or any names and it must ends with .page.link
.
The suffix can have the value SHORT
or UNGUESSABLE
.
Ouo.io
Driver that runs on Ouo.io API and currently using Version 1. To access the Ouo.io driver in this package,
provide the API token
that is available in the Ouo.io account dashboard.
This driver has monetization support where each link that has been clicked will earn a small amount of cash.
Changelog
Please see CHANGELOG for more information what has been changed in recent versions.
Security
If you discover any security related issues, please email one of the authors instead of using the issue tracker. You can find the author emails in the composer.json.
Credits
License
The MIT License (MIT). Please see License File for more information.