ashkanrimer / hashids
从数字生成短、唯一、非顺序的id(如YouTube和Bitly)
Requires
- php: ^8.0
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.5
Suggests
- ext-bcmath: Required to use BC Math arbitrary precision mathematics (*).
- ext-gmp: Required to use GNU multiple precision mathematics (*).
README
Hashids 是一个小的PHP库,可以从数字生成类似YouTube的id。当您不希望将数据库的数字id暴露给用户时使用它:https://hashids.org/php
这是一个围绕原始 hashids/hashids 包的包装器。有关基本文档,请参考原始版本。
https://github.com/vinkla/hashids
上述徽章是原始包的。
安装
使用Composer在项目的根目录中要求此包。
$ composer require dutymess/hashids
然后您就可以享受原始包的完整功能了。有关更多详细信息,请参阅他们的文档。
在此,我只是描述了添加了哪些功能。
相对于原始包的优点
- 它已针对最合适的用法进行了配置,字母和长度已预先设置。
- 编码器不会为单个id返回数组。您不必担心数组/字符串转换。
- 如果遇到麻烦,绕过器是一个很好的工具。
- 模型特质可以在eloquent模型上使用,并轻松提供hashid工具。
- 您仍然可以享受原始包提供的好处,而没有任何额外开销。
基本用法
您需要做的第一件事是在您的 .env 文件中定义您的盐。
HASHID_SALT_MAIN=SomeSaltSomeSaltSomeSaltSomeSaltSomeSaltSomeSaltSomeSalt
Hashids不应作为安全手段使用,但重新定义每个应用的盐是一个好习惯。
然后,您就可以通过简单的辅助函数将id和hashids相互转换。
hashid(1); // returns the hadhis of 1. ex: rolXo hashid('rolXo') // returns the equivalent id: 1
无论输入是什么,hashid 函数总是会切换。
如果您想确保返回的数据是所需类型,请使用以下方法。
hashid_number(1); // returns 1 hashid_number('rolXo'); // returns 1 hashid_string('rolXo'); // returns 'rolXo' hashid_string(1); // returns 'rolXo'
所有上述方法都接受数组作为参数。
hashid([1,2,3]); // returns ["rolXo", "bVnxk", "LkGjo"]
模型辅助函数
由于此包最重要的用法是隐藏表 id,因此我们引入了一个特殊的特质以供您在模型中使用。
<?php namespace App; use Hashids\HashidsModelTrait; use Illuminate\Database\Eloquent\Model; class User extends Model { use HashidsModelTrait; }
然后,您的模型将添加 hashid 访问器。
echo $post->id; // 1 echo $post->hashid; // rolXo
此外,还添加了一个查询作用域,可以轻松通过hashids而不是id来过滤记录
Post::where('title', 'foo')->withHadhis(["rolXo", "bVnxk", "LkGjo"]);
如果您在数组中包含真实id是完全可以的。即您不必确保接收到的所有内容都是hashids的形式。真实id和hashids的混合也可以。
Post::where('title','foo')->withHashids(["rolXo", 2, "3"])
⚠️永远不要在数据库中保存hashid。密钥可能会更改,您将丢失数据。
简易绕过
当hashids被激活时,调试应用程序可能会成为一个真正的头疼问题,因为您必须不断地在tinker中进行转换,以查看实际情况。
为了处理这个问题,您可以在您的 .env 文件中绕过机制。
HASHID_BYPASS=1
这将绕过真正的加密,只将id转换为等效字符串,以便您轻松阅读响应并跟踪可能的错误。
hashid(1); // returns 'h1'; hashid('h1'); // returns 1;
⚠️请务必仅在开发模式下执行此操作。
自定义盐
您可以定义自定义盐并在您的应用程序中自由使用它们。
首先,发布配置。
php artisan vendor:publish
将发布完整的注释配置文件,您可以定义所需的所有盐。
<?php
return [
/*
|--------------------------------------------------------------------------
| Salt
|--------------------------------------------------------------------------
|
| These are the salts of encryption. While the helper function use `main`
| as default salt, you can always pass custom salts too. Just be sure
| to define salts in your .env file and keep them unique over app.
|
*/
'salt' => [
"main" => env("HASHID_SALT_MAIN", null),
"custom" => env("HASHID_SALT_CUSTOM", null), // <~~ This is custom!
],
/*
|--------------------------------------------------------------------------
| Bypass
|--------------------------------------------------------------------------
|
| This is useful when you are in debug/development mode and you got sick
| of constantly converting hashids and ids to each other and see what
| is what. You can bypass mechanism by .env file to see real ids.
|
*/
"bypass" => (bool)env("HASHID_BYPASS", false),
];
上述辅助函数可以通过接收第二个参数来处理您的自定义盐。
hashid(1, "custom");
高级用法
在id和hashid之间轻松切换并不总是你想要的。虽然上述辅助函数旨在尽可能找到最合适的值,但你可能希望在用户提交id而不是hashid时出现错误。
以下方法不会自动转换内容。
use Hashids\Wrapper as Hashid;
Hashid::idEncode($matter, string $salt_name = "main");
Hashid::idDecode($matter, string $salt_name = "main")
此外,您可以使用以下方法对最小长度和字母进行更多控制
use Hashids\Wrapper as Hashid; Hashid::encode($matter, string $salt_name = "main", $min_length = 5, $alphabet = null); Hashid::decode(string $matter, string $salt_name = "main", $min_length = 5, $alphabet = null)
请注意,将null作为字母表传递将指示方法使用自定义字母表,该字母表仅由无数字的英语字母组成。
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
许可证
为了尊重原始维护者的努力,我未对任何许可证进行修改。