diezz/url-shortener 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

diezz/url-shortener

Composer 安装命令:

composer require diezz/url-shortener

包简介

UrlShortener plugin for CakePHP

README 文档

README

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.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

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.

Configuration

Define next section in the App config file

'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:

    Plugin::load('UrlShortener', ['bootstrap' => false, 'routes' => true]);

or just run

bin/cake plugin load UrlShortener

Usage

Create short url

    $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

    $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.

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

    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.

$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

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

You can set event listeners for each event for extending plugin functionality for example logging or counting hits.

EventManager::instance()->on(UrlShortener::EVENT_EXPAND_FAIL, function(Event $event) {
    $shortUrl = $event->data['shortUrl'];
    Log::write('error', 'Unable to expand url: ' . $shortUrl);
});

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固