toanld/hashids

Laravel的Hashids桥梁

v2.0.2 2023-10-18 09:08 UTC

This package is auto-updated.

Last update: 2024-09-18 11:08:24 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 toanld/hashids

配置

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

$ php artisan vendor:publish

这将在您的app中创建一个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的文档。