短码编解码
encode、decode 与十六进制编解码经 __call 落到默认连接。
核心层只做多连接解析与实例构建,不依赖任何框架;connection() 按需构建并缓存,未指定连接时经 __call() 转发到默认连接。
六个框架适配类合计约 490 行,只负责容器绑定与配置读取;Laravel 的 Facade 与配置格式对齐 vinkla/hashids。
一只圆角方块造型的蓝绿色小宠物,头顶天线,胸前有井号徽记,身旁气泡显示编码结果 3kTMd。
把数据库自增 ID 换成短小、不可猜测的字符串六个能力分组,围绕同一个零框架依赖内核。
encode、decode 与十六进制编解码经 __call 落到默认连接。
按 Salt 与 Length 配置多套连接,connection() 切换,构建一次即缓存。
Laravel、Webman、ThinkPHP、Hyperf、Yii2、Yii3 各有原生接线。
Facade、容器绑定与配置形状对齐 vinkla/hashids,可平滑替换。
核心只依赖 hashids/hashids,new HashidsManager 即可独立工作。
README 与架构图同步 12 种译文,接入前先看懂全貌。
PHP 版之外,也可以用 Rust 原生实现同一套短 ID 能力。
短 ID 的 Rust 原生实现
原生实现 hashids 算法,与官方测试向量一致,默认 feature 零第三方依赖,并适配八个 Rust Web 框架。
从 API 对齐到真实框架契约测试,每一步都有发布记录。
新增 Yii 2、Yii 3 适配,并把文档同步到 12 种语言与三张架构图。
给 Laravel 补上真实框架契约测试,拆开两个互相冲突的 CI 作业。
补上 Install 的端到端契约测试,堵住替身测试的最后一块盲区。
修复 Webman 覆盖配置与 Hyperf 配置模型,新增对齐 vinkla 的 API。
点击图片放大查看。
先安装,再跑一个最小示例。
composer require erikwang2013/hashidsuse Erikwang2013\Hashids\HashidsFactory;
use Erikwang2013\Hashids\HashidsManager;
$manager = new HashidsManager(
require __DIR__ . '/config/hashids.php',
new HashidsFactory()
);
$hash = $manager->encode(1, 2, 3);
$ids = $manager->decode($hash);cargo add hashids-rustuse std::sync::Arc;
use hashids::{Config, ConnectionConfig, Guard, HashidsManager};
let manager = Arc::new(HashidsManager::new(
Config::new().connection("main", ConnectionConfig::new().salt("this is my salt")),
));
let hashids = Guard::from_manager(manager)?;
let hash = hashids.encode(&[1, 2, 3]);
let ids = hashids.decode(&hash);鼠标停一停,它会换一种心情。