andrey-helldar / short-url
v2.2.0
2021-03-23 15:05 UTC
Requires
- php: ^7.2.5|^8.0
- andrey-helldar/laravel-support: ^1.1
- andrey-helldar/support: ^2.0|^3.0
- illuminate/database: ^5.5|^6.0|^7.0|^8.0
- illuminate/routing: ^5.5|^6.0|^7.0|^8.0
- illuminate/support: ^5.5|^6.0|^7.0|^8.0
- illuminate/validation: ^5.5|^6.0|^7.0|^8.0
Requires (Dev)
- mockery/mockery: ^0.9|^1.0
- orchestra/testbench: ^3.5|^4.0|^5.0|^6.0
- phpunit/phpunit: ^6.0|^7.0|^8.0|^9.0
Suggests
- symfony/thanks: Give thanks (in the form of a GitHub) to your fellow PHP package maintainers
README
用于 Laravel 框架的简单短网址生成器。
此包已被放弃,不再维护。作者建议使用
ashallendesign/short-url包。
安装
要获取 Laravel Short URL 的最新版本,只需使用 composer 引入项目。
composer require andrey-helldar/short-url
当然,您也可以手动更新您的 require 块,并运行 composer update。
{
"require": {
"andrey-helldar/short-url": "^2.0"
}
}
如果您不使用自动发现,请将 ServiceProvider 添加到 config/app.php 文件中的 providers 数组。
Helldar\ShortUrl\ServiceProvider::class;
您还可以发布配置文件以更改实现(例如,将接口更改为特定类)。
php artisan vendor:publish --provider="Helldar\ShortUrl\ServiceProvider"
现在您可以使用 Helldar\ShortUrl\Facades\ShortUrl 门面。
使用
标识符
在创建记录时,有两种方式来创建唯一的标识符:第一种是通过将数据库中的记录标识符转换为字母索引
| id | key | url | 输出url |
|---|---|---|---|
| 1 | b | https://example.com | https://my-site.com/go/b |
| 2 | c | https://example.com/foo | https://my-site.com/go/c |
| 3 | d | https://example.com/bar | https://my-site.com/go/d |
| ... | ... | ... | ... |
| 100 | dw | https://example.com/baz | https://my-site.com/go/dw |
| 200 | hs | https://example.com/qwe | https://my-site.com/go/hs |
| ... | ... | ... | ... |
| 1000 | bmm | https://example.com/rty | https://my-site.com/go/bmm |
| 5000 | hki | https://example.com/qax | https://my-site.com/go/hki |
| ... | ... | ... | ... |
第二种是基于当前微秒时间戳的唯一标识符
| id | key | url | 输出url |
|---|---|---|---|
| 1 | 5d35b1610705f679245100 | https://example.com | https://my-site.com/go/5d35b1610705f679245100 |
| 2 | 5d35b1727eb33156257300 | https://example.com/foo | https://my-site.com/go/5d35b1727eb33156257300 |
| 3 | 5d35c3193aaf8239852915 | https://example.com/bar | https://my-site.com/go/5d35c3193aaf8239852915 |
| ... | ... | ... | ... |
| 100 | 5d35c3195eaaa426635669 | https://example.com/baz | https://my-site.com/go/5d35c3195eaaa426635669 |
| 200 | 5d35c3199d7f7453462663 | https://example.com/qwe | https://my-site.com/go/5d35c3199d7f7453462663 |
| ... | ... | ... | ... |
| 1000 | 5d35c31a44398568656171 | https://example.com/rty | https://my-site.com/go/5d35c31a44398568656171 |
| 5000 | 5d35c31aca1c1975257906 | https://example.com/qax | https://my-site.com/go/5d35c31aca1c1975257906 |
| ... | ... | ... | ... |
set()
要创建记录,请使用 set() 方法。
use \Helldar\ShortUrl\Facades\ShortUrl; $item = ShortUrl::set('https://example.com'); // returned instance of `Helldar\ShortUrl\Models\Short` eloquent model.
get()
要获取记录,请使用 get() 方法。
use \Helldar\ShortUrl\Facades\ShortUrl; $url = ShortUrl::get('foo'); // returned URL string. // For example, 'http://<your_site>.com/go/qdr'.
路由
要创建路由,可以使用以下代码
use \Helldar\ShortUrl\Facades\ShortUrl; $item = ShortUrl::set('https://example.com'); return route('short_url', ['key' => $item->key]);
或
use \Helldar\ShortUrl\Facades\ShortUrl; $url = ShortUrl::get('foo'); return redirect()->away($url);
您还可以在包设置中更改路由的名称 更改路由名称。
Blade 模板
您还可以从模板引擎调用门面。
<a href="{{ \Helldar\ShortUrl\Facades\ShortUrl::get('foo') }}">open link</a> {{-- <a href="http://your-site.com/go/foo">open link</a> --}}
或
<a href="{{ \Helldar\ShortUrl\Facades\ShortUrl::set('http://example.com') }}">open link</a> {{-- <a href="http://your-site.com/go/cr">open link</a> --}}
许可证
本软件包遵循MIT 许可协议。
企业版
作为 Tidelift 订阅的一部分提供。
andrey-helldar/short-url的维护者以及成千上万的其他软件包的维护者正在与Tidelift合作,为您提供您构建应用程序时使用的开源软件的商业支持和维护。通过支付您所使用软件包的维护者,您可以节省时间、降低风险并提高代码质量。了解更多信息。