diezz / url-shortener
CakePHP 的 URL 缩短插件
Requires
- php: >=7.1
- cakephp/cakephp: ^3.4
- league/uri: ^4
Requires (Dev)
- phpunit/phpunit: ^5.7|^6.0
This package is not auto-updated.
Last update: 2024-09-29 05:09:14 UTC
README
此插件允许缩短长 URL。例如,您有一个这样的长 URL:
http://domain.com/events/follow?event_id=3D8e296a067a37563370ded05f5a3bf3ec&refer=IuyNqrcLQaqPhjzhFiCARg__.3600 .1282755600-761405628%26fb_sig_ss%3DigFqJKrhJZWGSRO
此插件可以将此 URL 缩短为:
http://domain.com/NjdhMz
用户点击此短 URL 后,插件将您重定向到原始 URL。
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require diezz/url-shortener
注意
此包有 DataProviderInterface 依赖项,其实现未提供。您应该在您的源代码中实现 DataProviderInterface 并在 App 配置文件中提供它。您可以使用任何喜欢的外部存储:Redis、Mongo、MySql、文件等。为了防止冲突,如果提供的键不是唯一的,则 DataProviderInterface::put() 方法应抛出 UrlShortener\DuplicateKeyException。
配置
在 App 配置文件中定义以下部分
'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', ]
在 config/bootstrap.php 的末尾写入以下行
Plugin::load('UrlShortener', ['bootstrap' => false, 'routes' => true]);
或者直接运行
bin/cake plugin load UrlShortener
使用方法
创建短 URL
$urlShortener = new UrlShortener(); $shortUrl = $urlShortener->shorten('https://domain.com/some_mega_supper_pupper_long_url'); // $shortUlr = 'http://domain.com/l/MnNQLC'
您可以手动设置短 URL
$shortUrl = $urlShortener->shorten('https://domain.com/some_mega_supper_pupper_long_url', 'one'); // $shortUlr = 'http://domain.com/l/one'
插件提供了两种扩展完整 URL 的方法。
//expand full url in controller $fullUrl = $urlShortener->expandByRequest($this->request); //expand url anywhere $fullUrl = $urlShortener->expandByHash($shortUrlHash);
插件包含 Facade 类,方便使用 UrlShortener
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);
插件有默认的短 URL 哈希生成器,但您可以通过传递回调到 setHashGenerator 方法来定义自定义生成器。
$urlShortener = new UrlShortener(); $urlShortener->setHashGenerator(function($fullUrl) { return uniqid($fullUrl); });
另一种设置用户定义生成器的方法是在 UrlShortener 配置中定义它
//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 支持以下事件
url.shortener.event.before.expandurl.shortener.event.after.expandurl.shortener.event.expand.failurl.shortener.event.before.shortenurl.shortener.event.after.shortenurl.shortener.event.shorten.fail
您可以为每个事件设置事件监听器,以扩展插件的功能,例如记录或计算点击次数。
EventManager::instance()->on(UrlShortener::EVENT_EXPAND_FAIL, function(Event $event) { $shortUrl = $event->data['shortUrl']; Log::write('error', 'Unable to expand url: ' . $shortUrl); });