PHPackages                             isszz/think-hashids - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. isszz/think-hashids

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

isszz/think-hashids
===================

Thinkphp Hashids Used to generate a youtube-like ID from a digital ID

v0.0.1(2y ago)216MITPHPPHP &gt;=8.0.0

Since Jul 27Pushed 2y ago1 watchersCompare

[ Source](https://github.com/isszz/think-hashids)[ Packagist](https://packagist.org/packages/isszz/think-hashids)[ RSS](/packages/isszz-think-hashids/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

think-hashids
=============

[](#think-hashids)

- Thinkphp 中使用 Hashids 用于将数字ID生成类似YouTube的ID。当您不想向用户公开数据库数字ID时使用
- 支持B站的ID生成模式，生成B站/video/`BV1fx411v7eo`这种ID

 [![Minimum PHP Version](https://camo.githubusercontent.com/e8223ac675b6f9feddec95a806a57528e8009d21035aca7c0650e6006fa81c36/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d3e3d382e302d3838393242462e737667)](https://packagist.org/packages/isszz/think-hashids) [![Minimum Thinkphp Version](https://camo.githubusercontent.com/2bc52c123b7ec544298bd4aa257f4efb62df5ce49bfa1f6fdfadc2e20cfad61a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7468696e6b7068702d3e3d362e782d3838393242462e737667)](https://packagist.org/packages/isszz/think-hashids) [![Stable Version](https://camo.githubusercontent.com/b57aea450fa75c62c3fc87614ac2739baeaee80038cdc43b20ac2150a4c97187/68747470733a2f2f706f7365722e707567782e6f72672f6973737a7a2f7468696e6b2d686173686964732f762f737461626c65)](https://packagist.org/packages/isszz/think-hashids) [![Total Downloads](https://camo.githubusercontent.com/d3c1b27aa44ac5b33a95d8906ffb9df3c19cd74191f4b9357059fb25cf2f62e6/68747470733a2f2f706f7365722e707567782e6f72672f6973737a7a2f7468696e6b2d686173686964732f646f776e6c6f616473)](https://packagist.org/packages/isszz/think-hashids) [![License](https://camo.githubusercontent.com/c866b9331f2e4740976a31e273eb699fe5fb26b2064916825ad19f8b7c21564d/68747470733a2f2f706f7365722e707567782e6f72672f6973737a7a2f7468696e6b2d686173686964732f6c6963656e7365)](https://packagist.org/packages/isszz/think-hashids)

安装
--

[](#安装)

```
composer require isszz/think-hashids
```

配置
--

[](#配置)

在 config/hashids.php 中更改

```
return [
    // 默认连接名称
    'default' => 'main', // 支持bilibili的BV模式

    // Hashids modes
    'modes' => [
        'main' => [
            'salt' => '',
            'length' => 0,
        ],
        'other' => [
            'salt' => 'salt',
            'length' => 0,
            'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
        ],
        'bilibili' => [
            // 此模式无需添加其他的配置
            // 前缀超过2位英文字母忽略
            'prefix' => ['', ''], // B站BV模式前缀类似: BV1fx411v7eo = 12345678
        ],
    ],
];
```

用法
--

[](#用法)

**facade方式引入**

```
use isszz\hashids\facade\Hashids;

class Index
{
    public function index()
    {
        // B站BV模式, B站模式支持第二个参数增加前缀，可以设置例如: prefix = 'BV'
        Hashids::mode('bilibili')->encode(12345678); // 1fx411v7eo
        Hashids::mode('bilibili')->decode('1fx411v7eo'); // 12345678

        // other模式
        Hashids::mode('other')->encode(12345678); // gpyAoR
        Hashids::mode('other')->decode('gpyAoR'); // 12345678

        // 默认
        Hashids::encode(12345678); // 1rQ2go
        Hashids::decode('1rQ2go'); // 12345678

        // 更改默认模式为bilibili
        Hashids::setDefaultMode('bilibili');

        Hashids::encode(12345678); // 1fx411v7eo
        Hashids::decode('1rQ2go'); // 12345678

        // 其他传输ID的方式，返回为数组，对应传参
        Hashids::encode(12, 34, 56, 78); // nyILSjosbR
        $hashID = Hashids::encode([12, 34, 56, 78]); // nyILSjosbR

        $result = Hashids::decode($hashID);
        // 返回数组，对应传入参数
        /*
        $result = [
            '0' => 12
            '1' => 34
            '2' => 56
            '3' => 78
        ];
        */
    }
}
```

**依赖注入方式**

```
use isszz\hashids\Hashids;

class Index
{
    public function index(Hashids $hashids)
    {
        // B站BV模式, B站模式支持第二个参数增加前缀，可以设置例如: prefix = 'BV'
        $hashids->mode('bilibili')->encode(12345678); // 1fx411v7eo
        $hashids->mode('bilibili')->decode('1fx411v7eo'); // 12345678

        // other模式
        $hashids->mode('other')->encode(12345678); // gpyAoR
        $hashids->mode('other')->decode('gpyAoR'); // 12345678

        // 默认
        $hashids->encode(12345678); // 1rQ2go
        $hashids->decode('1rQ2go'); // 12345678

        // 更改默认模式为bilibili
        $hashids->setDefaultMode('bilibili');
    }
}
```

**助手函数**

```
class Index
{
    public function index()
    {
        // 加密
        id_encode(12345678); // 1rQ2go
        id_encode(12, 34, 56, 78, 'other'); // nyILSjosbR
        id_encode([12, 34, 56, 78], mode: 'other'); // nyILSjosbR

        // 解密
        id_decode('1rQ2go'); // 12345678
        id_decode('gpyAoR', 'other'); // 12345678

        // 切换模式
        id_mode('other')->encode(12345678); // gpyAoR
        id_mode('other')->decode('gpyAoR'); // 12345678

        // 助手函数还有一个获取字母表的函数
        // 拿到可以用来设置`config/plugin/isszz/webman-hashids/app.php `配置中的alphabet字段
        $alphabet = id_build_alphabet();
    }
}
```

**使用模型获取器对ID进行加密**

```
public function getIdAttr($value)
{
    return id_encode($value);
}

// 主键非id时, 比如是tid时
public function getTidAttr($value)
{
    return id_encode($value);
}
```

- 基础库来自: [vinkla/hashids](https://github.com/vinkla/hashids)

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1024d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/69a944bc69c24c6b069b99b047f791b359844dbc71a42404fbfd6ef3b484c05f?d=identicon)[isszz](/maintainers/isszz)

---

Top Contributors

[![isszz](https://avatars.githubusercontent.com/u/2878463?v=4)](https://github.com/isszz "isszz (12 commits)")

---

Tags

hashidsthinkphpthink

### Embed Badge

![Health badge](/badges/isszz-think-hashids/health.svg)

```
[![Health](https://phpackages.com/badges/isszz-think-hashids/health.svg)](https://phpackages.com/packages/isszz-think-hashids)
```

###  Alternatives

[vinkla/hashids

A Hashids bridge for Laravel

2.1k13.3M73](/packages/vinkla-hashids)[elfsundae/laravel-hashid

A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

415246.3k2](/packages/elfsundae-laravel-hashid)[deligoez/laravel-model-hashid

Generate, Save, and Route Stripe/Youtube-like Hash IDs for Laravel Eloquent Models

16498.0k](/packages/deligoez-laravel-model-hashid)[balping/laravel-hashslug

Package providing a trait to use Hashids on a model

25185.2k2](/packages/balping-laravel-hashslug)[cayetanosoriano/hashids-bundle

Bundle for integration of hashids lib to the container

22166.0k](/packages/cayetanosoriano-hashids-bundle)[isszz/rotate-captcha

Rotate image captcha

801.7k](/packages/isszz-rotate-captcha)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
