UrlShortener plugin for CakePHP
This plugin allow to shorten long urls. For example you have a long url like this
http://domain.com/events/follow?event_id=3D8e296a067a37563370ded05f5a3bf3ec&refer=IuyNqrcLQaqPhjzhFiCARg__.3600
.1282755600-761405628%26fb_sig_ss%3DigFqJKrhJZWGSRO
This plugin can shorten this url to
http://domain.com/NjdhMz
and after user follow this short url plugin will redirect you to original url., (*1)
Installation
You can install this plugin into your CakePHP application using composer., (*2)
The recommended way to install composer packages is:, (*3)
composer require diezz/url-shortener
Note
This package has DataProviderInterface dependency which implementation is not provided. You
should implement DataProviderInterface
in your
source and provide it in the App config file.
You may use any external storage that you like: Redis, Mongo, MySql, files and so on.
In order to prevent collisions DataProviderInterface::put()
method should throw UrlShortener\DuplicateKeyException
if presented key is not unique., (*4)
Configuration
Define next section in the App config file, (*5)
'UrlShortener' => [
// Requeried. Data Provider Implementation.
// Full class name should be provided.
'dataProveder' => YourImplementation::class,
// Optional. Short url hash length. By default 6
'urlLength' => 6,
// Optional. Whether retry if catch DuplicateKeyException
// By default false
'retryOnDuplicate' => false,
//Optioanal. Base url of short url.
// If this param not set App.appBaseUrl will be used as baseUrl
'baseUrl' => 'http://domain.com',
// Optional. Short url path. By default null,
// This value will be inserted between base url and short url hash
// and short url will be looking like this http://domain.com/l/MnNQLC
'shortUrlPath' => 'l',
]
at the end of config/bootstrap.php
write next lines:, (*6)
Plugin::load('UrlShortener', ['bootstrap' => false, 'routes' => true]);
or just run, (*7)
bin/cake plugin load UrlShortener
Usage
Create short url, (*8)
$urlShortener = new UrlShortener();
$shortUrl = $urlShortener->shorten('https://domain.com/some_mega_supper_pupper_long_url');
// $shortUlr = 'http://domain.com/l/MnNQLC'
You can set manually short url, (*9)
$shortUrl = $urlShortener->shorten('https://domain.com/some_mega_supper_pupper_long_url', 'one');
// $shortUlr = 'http://domain.com/l/one'
Plugin is providing two methods for expanding full url., (*10)
//expand full url in controller
$fullUrl = $urlShortener->expandByRequest($this->request);
//expand url anywhere
$fullUrl = $urlShortener->expandByHash($shortUrlHash);
Plugin contain Facade class for convenient UrlShortener usage, (*11)
use UrlShortener/Facade as UrlShortener;
//create short url
$shortUrl = UrlShortener::shorten('https://domain.com/some_mega_supper_pupper_long_url');
//expand full url
$fullUrl = UrlShortener::expandByRequest($this->request);
Plugin have default short url hash generator but you may define custom generator by passing callback into
setHashGenerator
method., (*12)
$urlShortener = new UrlShortener();
$urlShortener->setHashGenerator(function($fullUrl) {
return uniqid($fullUrl);
});
Other way to set user defined generator is to define it in the UrlShortener config, (*13)
//Define some class in your sorce
class Generator {
public static generate(string $fullUrl): string
{
return uniqid($fullUrl);
}
}
//app config
'UrlShortener' => [
...
'hashGenerator' => [Generator::class, 'generate']
]
UrlShortener support next events:
- url.shortener.event.before.expand
- url.shortener.event.after.expand
- url.shortener.event.expand.fail
- url.shortener.event.before.shorten
- url.shortener.event.after.shorten
- url.shortener.event.shorten.fail
, (*14)
You can set event listeners for each event for extending plugin functionality for example logging or counting hits., (*15)
EventManager::instance()->on(UrlShortener::EVENT_EXPAND_FAIL, function(Event $event) {
$shortUrl = $event->data['shortUrl'];
Log::write('error', 'Unable to expand url: ' . $shortUrl);
});