little-superman/hyperf-hashids

v0.0.1 2021-03-11 11:39 UTC

This package is auto-updated.

Last update: 2024-09-11 19:54:15 UTC


README

hashids

Hyperf Hashids

Hyperf 的 Hashids 桥接器。

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

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

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

安装

使用 Composer 在项目的根目录中安装此包。

$ composer require "little-superman/hyperf-hashids:dev-main"

配置

Hyperf Hashids 需要连接配置。要开始,你需要发布所有供应商资产

$ php bin/hyperf.php vendor:publish hyperf/hashids

这将在你的应用中创建一个 config/hashids.php 文件,你可以修改它来设置你的配置。同时,请确保检查该包在版本发布之间的原始配置文件的变化。

默认连接名称

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

Hashids 连接

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

使用方法

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

// You can alias this in config/app.php.
use LittleSuperman\HyperfHashids\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 LittleSuperman\HyperfHashids\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 LittleSuperman\HyperfHashids\Contract\HashidsInterface;

class Foo
{
    protected $hashids;

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

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

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