laracrafts/laravel-url-shortener
Composer 安装命令:
composer require laracrafts/laravel-url-shortener
包简介
Powerful URL shortening tools in Laravel
README 文档
README
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 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, ... ],
Usage
The shortener can be retrieved from the container in two ways:
$shortener = app('url.shortener'); // or... $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 url()->shorten(...); // or... app('url')->shorten(...); // or even... app('url.shortener')->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 Laravel's 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).
public function boot(ShortenerManager $shorteners) { $shorteners->extend('my_driver', function ($app, $config) { // Return your driver instance here }); }
Once you have registered your driver you can call it like any other driver.
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 |
| Polr | polr |
0.3.0 | yes | no |
| 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
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 |
|---|---|
URL_SHORTENER_API_TOKEN |
Your Bit.ly API token |
URL_SHORTENER_PREFIX |
Your short URL domain |
Firebase Dynamic Links
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 |
|---|---|---|
URL_SHORTENER_API_TOKEN |
Your Firebase API token | |
URL_SHORTENER_PREFIX |
Your URL prefix | |
URL_SHORTENER_STRATEGY |
The path component creation method | UNGUESSABLE |
Is.gd / V.gd
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 |
|---|---|
URL_SHORTENER_ANALYTICS |
Enable or disable statistics |
Ouo.io
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 |
|---|---|
URL_SHORTENER_API_TOKEN |
Your Ouo.io API token |
Polr
This driver uses the Polr API. The API requires an access token and a URI prefix.
| Variable | Description |
|---|---|
URL_SHORTENER_API_TOKEN |
Your Polr API token |
URL_SHORTENER_PREFIX |
Your URL prefix |
Shorte.st
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 |
|---|---|
URL_SHORTENER_API_TOKEN |
Your Shorte.st API token |
TinyURL
This driver runs on the TinyURL API, which requires no additional setup. This driver is the package default.
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.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
laracrafts/laravel-url-shortener 适用场景与选型建议
laracrafts/laravel-url-shortener 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112.27k 次下载、GitHub Stars 达 97, 最近一次更新时间为 2019 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「url」 「shortener」 「shortening」 「laravel」 「bit.ly」 「tinyurl」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laracrafts/laravel-url-shortener 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laracrafts/laravel-url-shortener 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laracrafts/laravel-url-shortener 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
url shorten package using Laravel
A Laravel package to shorten urls
Easy URL rewrites in your Laravel application
Powerful URL shortening tools in Laravel
Laravel 7 local url shortener
A Laravel helper to detect if the current route/path is active.
统计信息
- 总下载量: 112.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 98
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-17