supernova3339/laravel-hashids

Laravel 的 Hashids 桥接器

10.0.1 2022-04-10 18:38 UTC

README

hashids

Laravel Hashids

为 Laravel 提供的 Hashids 桥接器。

// Encode integers.
Hashids::encode(4815162342);

// Decode strings.
Hashids::decode('1LLb3b4ck');

// Dependency injection example.
$hashidsManager->encode(911);

Build Status Monthly Downloads Latest Version

安装

使用 Composer,在项目根目录下要求安装此包。

composer require vinkla/hashids

配置

Laravel Hashids 需要连接配置。要开始,您需要发布所有供应商资源

$ php artisan vendor:publish

这将在您的应用中创建一个 config/hashids.php 文件,您可以修改该文件以设置配置。同时,请确保您检查此包中原始配置文件之间的更改。

默认连接名称

此选项 default 是您指定要使用的以下哪个连接作为所有工作的默认连接的地方。当然,您可以使用管理器类同时使用多个连接。此设置的默认值是 main

Hashids 连接

此选项 connections 是为您的应用程序设置每个连接的地方。已包括示例配置,但您可以添加尽可能多的连接。

使用方法

在这里,您可以看到如何使用此包的示例。默认情况下,默认适配器是 main。在您在配置文件中输入您的认证详细信息后,它将正常工作

// You can alias this in config/app.php.
use Vinkla\Hashids\Facades\Hashids;

// We're done here - how easy was that, it just works!
Hashids::encode(4815162342);

// This example is simple and there are far more methods available.
Hashids::decode('doyouthinkthatsairyourebreathingnow');

管理器将表现得像一个 Hashids\Hashids 类。如果您想调用特定的连接,可以使用连接方法

use Vinkla\Hashids\Facades\Hashids;

// Writing this...
Hashids::connection('main')->encode($id);

// ...is identical to writing this
Hashids::encode($id);

// and is also identical to writing this.
Hashids::connection()->encode($id);

// This is because the main connection is configured to be the default.
Hashids::getDefaultConnection(); // This will return main.

// We can change the default connection.
Hashids::setDefaultConnection('alternative'); // The default is now alternative.

如果您更愿意使用依赖注入而不是外观,则可以注入管理器

use Vinkla\Hashids\HashidsManager;

class Foo
{
    protected $hashids;

    public function __construct(HashidsManager $hashids)
    {
        $this->hashids = $hashids;
    }

    public function bar($id)
    {
        $this->hashids->encode($id);
    }
}

App::make('Foo')->bar();

有关如何使用 Hashids\Hashids 类的更多信息,请参阅 hashids/hashids 的文档。