shivella / laravel-bitly
Laravel生成Bitly短链接的包
1.1.15
2024-02-27 17:34 UTC
Requires
- php: >=7.1
- ext-json: *
- guzzlehttp/guzzle: ~6.0 || ^7.0.1 || ^7.1 || ^7.2 || ^7.3 || ^7.4 || ^7.5
- illuminate/support: ^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- phpunit/phpunit: ^9.5 || ^10.5
- symfony/http-foundation: ^5.0
README
用于生成Bitly短链接的Laravel包。
更多详细信息请参阅Bitly
需求
Laravel 5.1或更高版本
安装
安装是一个快速的三步过程
- 使用composer下载laravel-bitly
- 在app.php中启用该包
- 配置您的Bitly凭证
- (可选) 配置包门面
步骤 1: 使用composer下载laravel-bitly
运行以下命令添加shivella/laravel-bitly
composer require shivella/laravel-bitly
步骤 2: 在app.php中启用该包
在:config/app.php中注册服务
Shivella\Bitly\BitlyServiceProvider::class,
步骤 3: 配置Bitly凭证
php artisan vendor:publish --provider="Shivella\Bitly\BitlyServiceProvider"
在您的.env文件中添加以下内容
BITLY_ACCESS_TOKEN=your_secret_bitly_access_token
步骤 4 (可选): 配置包门面
在:config/app.php中注册Bitly门面
<?php return [ 'aliases' => [ 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, // ... 'Bitly' => Shivella\Bitly\Facade\Bitly::class, ], // ... ];
用法
<?php $url = app('bitly')->getUrl('https://www.google.com/'); // http://bit.ly/nHcn3
或者如果您想使用门面,在类声明后添加以下内容
<?php use Bitly;
然后您可以直接通过调用Bitly::
来使用它
<?php $url = Bitly::getUrl('https://www.google.com/'); // http://bit.ly/nHcn3
测试
在您的单元测试中,您可以使用BitlyClientFake
类而不是常规客户端。它将使用散列创建假短链接,而不调用外部REST API,这将加快您的单元测试速度。可以通过DI在您的\Tests\TestCase::createApplication()
实现中设置假数据。
<?php namespace Tests; use Illuminate\Contracts\Console\Kernel; use Shivella\Bitly\Testing\BitlyClientFake; trait CreatesApplication { /** * Creates the application. * * @return \Illuminate\Foundation\Application */ public function createApplication() { $app = require __DIR__.'/../bootstrap/app.php'; $app->make(Kernel::class)->bootstrap(); // swap Bitly client by a fake $app->singleton('bitly', function () { return new BitlyClientFake(); }); return $app; } }
作为替代,您也可以使用\Shivella\Bitly\Facade\Bitly::fake()
方法将常规客户端替换为假客户端。