torann/hashids

Laravel 对 Hashids 的扩展包

3.0.5 2024-03-22 18:47 UTC

This package is auto-updated.

Last update: 2024-09-22 19:56:09 UTC


README

Latest Stable Version Total Downloads

此包使用了由 hashids.org 创建的类

从数字生成哈希,例如 YouTube 或 Bitly。当您不希望向用户暴露数据库 ID 时,请使用 hashids。

安装

Composer

从命令行运行以下命令

$ composer require torann/hashids

无需包自动发现

一旦 Hashids 安装完毕,您需要将服务提供者和外观注册到应用程序中。打开 config/app.php 并找到 providersaliases 键。

'providers' => [
    Torann\Hashids\HashidsServiceProvider::class,
]

'aliases' => [
    'Hashids' => Torann\Hashids\Facade\Hashids::class,
]

发布配置

在项目的根目录下从命令行运行此命令

$ php artisan vendor:publish --provider="Torann\Hashids\HashidsServiceProvider"

配置文件将被发布到 config/hashids.php

使用方法

遵循所有步骤并完成安装后,您可以使用 Hashids。

编码

您可以简单地编码单个 ID

Hashids::encode(1); // Returns Ri7Bi

或多个...

Hashids::encode(1, 21, 12, 12, 666); // Returns MMtaUpSGhdA

解码

Hashids::decode(Ri7Bi);

// Returns
array (size=1)
0 => int 1

或多个...

Hashids::decode(MMtaUpSGhdA);

// Returns
array (size=5)
0 => int 1
1 => int 21
2 => int 12
3 => int 12
4 => int 666

Hashids 的所有功劳都属于 Ivan Akimov (@ivanakimov),感谢他创建了这个工具!