shivella/laravel-bitly

Laravel生成Bitly短链接的包

1.1.15 2024-02-27 17:34 UTC

This package is auto-updated.

Last update: 2024-09-13 19:17:12 UTC


README

用于生成Bitly短链接的Laravel包。

更多详细信息请参阅Bitly

Build Status Latest Stable Version License Total Downloads

需求

Laravel 5.1或更高版本

安装

安装是一个快速的三步过程

  1. 使用composer下载laravel-bitly
  2. 在app.php中启用该包
  3. 配置您的Bitly凭证
  4. (可选) 配置包门面

步骤 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()方法将常规客户端替换为假客户端。