cndrsdrmn/short-url
Composer 安装命令:
composer require cndrsdrmn/short-url
包简介
A lightweight and flexible Laravel package for generating and managing short URLs.
README 文档
README
A lightweight and flexible Laravel package for generating and managing short URLs. This package simplifies the process of creating short links with customizable options, middleware, and bot protection.
Features
- 📏 Customizable token formats and lengths: Choose from mixed, numeric, or alphabetic tokens.
- 🛡 Bot protection: Optionally block requests from known bots.
- ⚡ Quick setup: Easily integrate into existing Laravel projects.
- 🔒 Token encryption: Secure your tokens with a custom encryption callback.
- 🎛 Middleware support: Add custom middleware to short URL routes.
Installation
Install the package via Composer:
composer require cndrsdrmn/short-url
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=short-url-config
The configuration file config/short-url.php will be published. It includes the following options:
return [ /* |-------------------------------------------------------------------------- | URL Prefix |-------------------------------------------------------------------------- | | This value sets the prefix for the short URL routes. It will be used as the | base path for accessing the short URLs. | */ 'prefix' => '/s', /* |-------------------------------------------------------------------------- | Allowed Schemas |-------------------------------------------------------------------------- | | Specify the schema can be allowed for generated token. | */ 'allowed_schemas' => [ 'http', 'https' ], /* |-------------------------------------------------------------------------- | Database Connection |-------------------------------------------------------------------------- | | Specify the database connection to be used for storing short URLs. If null, | the default connection defined in the database configuration will be used. | */ 'connection' => null, /* |-------------------------------------------------------------------------- | Middleware |-------------------------------------------------------------------------- | | Define the middleware to apply to the short URL routes. By default, the routes | are throttled to prevent abuse. You can modify or extend the middleware stack | as needed. | */ 'middleware' => [ 'throttle:100,1', ], /* |-------------------------------------------------------------------------- | Token Format |-------------------------------------------------------------------------- | | Specify the format used to generate tokens for short URLs. Supported values: | - "bothify": Mix of letters and numbers (e.g., `A1b2C`) | - "numerify": Numbers only (e.g., `12345`) | - "lexify": Letters only (e.g., `ABCDE`) | */ 'token_format' => 'bothify', /* |-------------------------------------------------------------------------- | Token Length |-------------------------------------------------------------------------- | | Define the length of the token generated for the short URLs. The token's | length determines how many characters will be included in the generated URL. | */ 'token_length' => 5, /* |-------------------------------------------------------------------------- | Block Bots |-------------------------------------------------------------------------- | | If enabled, requests from known bots will be blocked from accessing the | short URLs. This is useful for preventing automated access and ensuring | accurate analytics. | */ 'should_block_bots' => true, ];
Usage
Basic Usage
Shorten a URL
You can easily generate a short URL with the ShortUrl facade:
use Cndrsdrmn\ShortUrl\Facades\ShortUrl; $shortUrl = ShortUrl::shorten('https://example.com'); echo $shortUrl; // Outputs the short URL
Advanced Usage
Customizing the Builder
Customize your short URL with the make method:
use Cndrsdrmn\ShortUrl\Facades\ShortUrl; $builder = ShortUrl::make('https://example.com', [ 'is_single_use' => true, ]); $shortUrl = $builder->create(); echo $shortUrl->accessLink;
Setting Custom Headers and Queries
$builder = ShortUrl::make('https://example.com') ->headers(['X-Custom-Header' => 'value']) ->queries(['utm_source' => 'newsletter']); $shortUrl = $builder->create(); echo $shortUrl->accessLink;
Routes
By default, the package automatically registers a route for handling short URL redirections. If you want to customize the routing:
- Disable route registration on boot in
App\Providers\AppServiceProvider:ShortUrl::ignoreRoutes(); - Define your own route in
routes/web.php:use App\Http\Controllers\RedirectController; Route::get('/s/{token}', [RedirectController::class, 'handle']);
Security
Encrypt Tokens
Set a custom encryption callback to secure your tokens:
ShortUrl::encryptTokenUsing(function ($token) { return encrypt($token); });
Testing
Run the tests with PHPUnit:
php artisan test
License
This package is open-sourced software licensed under the MIT license.
Contributing
Contributions are welcome! Please see more detail on CONTRIBUTING.md.
Credits
- Author: Candra Sudirman
- Inspired by: URL shortening services like Bitly.
cndrsdrmn/short-url 适用场景与选型建议
cndrsdrmn/short-url 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「url」 「laravel」 「short」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cndrsdrmn/short-url 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cndrsdrmn/short-url 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cndrsdrmn/short-url 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy URL rewrites in your Laravel application
A Laravel helper to detect if the current route/path is active.
Provides a service and twig extension for getting short urls like http://your.host/~short
Alfabank REST API integration
SURL - Laravel Short Url
CakePHP Bitly Plugin
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-02