circlesc / laravel-hashids
自动从数字生成短、唯一、非顺序的ID(如YouTube和Bitly),用于Eloquent模型。
Requires
- php: ^8.1
- hashids/hashids: ^5.0
- illuminate/support: ^11.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
TL;DR
不要在界面或URI中显式显示数据的ID。使用模型上的Trail自动加密ID,同时,它还可以正确地解密你想注入到路由中的类实例。它还提供了当你需要时加密和解密ID的快速方法。它提供了一些测试命令。灵感来自 vinkla/hashids。
入门指南
通过Composer安装
使用Composer在项目的根目录中要求此包。
composer require cirlmcesc/laravel-hashids
配置文件
Artisan命令将发布到config/
。可以通过修改参数和属性来自定义操作模式。
php artisan hashids:install
用法
在模型中使用
使用模型特质,你可以快速使用它。在模型上使用特质,你可以在模型序列化时自动加密ID,并且当你设置了$_only_need_encode_fields
字段的值时,你也可以使用它。如果没有设置此属性,所有以_id
结尾的字段都将自动加密。你可以选择设置$_doesnt_need_encode_fields
属性以防止这些字段被加密。然而,$_only_need_encode_fields
和$_doesnt_need_encode_fields
是互斥的。如果两者都设置了,当模型序列化为数组时将抛出异常。当然,你也可以选择设置$_only_encode_id
属性以确定是否仅加密ID字段。如果需要加密额外的字段,并且字段不以_id
结尾,则必须设置$_only_encode_fields
以包括所有以_id
结尾的字段以及需要额外加密的字段。只有以这种方式才能正常工作。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Cirlmcesc\LaravelHashids\Traits\Hashidsable; class Foo extends Model { use Hashidsable; /** * Encrypt the list of other fields that * need to be encrypted while encrypting the ID. * * @var bool */ public $_only_encode_id = false; /** * When encrypting an ID, you can choose to * Only encrypt which fields. * You can also set some fields with suffixes * other than `_id`, but they must be of type `int`. * * @var array<string> */ public $_only_need_encode_fields = [ 'aaa', 'bbb', 'ccc_id', 'ddd_id', ]; /** * While encrypting the ID, you can choose * which fields do not encryption, * Only applicable to fields with suffix 'id'. * * @var array<string> */ public $_doesnt_need_encode_fields = [ 'xxx_id', 'yyy_id', 'zzz_id', ]; }
在路由中使用
当将模型ID注入到路由或控制器操作时,它将自动解码ID。不需要采取任何额外操作。
<?php use Illuminate\Support\Facades\Route; use App\Models\Foo; Route::get('/foos/{foo}', fn(Foo $foo) => $foo);
在资源类中使用
当你使用资源类时,模型序列化不会通过模型特质进行,因此你需要加密资源类的toArray
方法中的ID。使用hashids_encode_in_array
方法可以对ID字段进行批量加密。
<?php namespace App\Http\Resources; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class FooResource extends JsonResource { /** * Transform the resource into an array. * * @return array<string, mixed> */ public function toArray(Request $request): array { return hashids_encode_in_array([ 'id' => $this->id, 'foo' => $this->foo, 'app_id' => $this->app_id, 'banana_id' => $this->banana_id, ]); } }
在其他地方使用函数
快速加密和解密。提供了一些函数。
<?php /** * encode id * * @param int $id * @return string */ function hashids_encode(int $id): string; /** * decode id * * @param string $id * @param int $remedy * @return int */ function hashids_decode(string $id, int $remedy = 0): int; /** * encode ids in array * * @param array $data * @param array $dosent_encode_keys * @param string $suffix * @return array */ function hashids_encode_in_array(array $data, array $dosent_encode_keys = [], string $suffix = '_id'): array; /** * decode ids in array * * @param array $data * @param array $dosent_decode_keys * @param string $suffix * @return array */ function hashids_decode_in_array(array $data, array $dosent_decode_keys = [], string $suffix = '_id'): array;
测试命令 Artisan命令可以在命令行中使用加密和解密。
php artisan hashids:test