rawilk / human-keys
为您的模型使用类似Stripe的ID。
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/contracts: ^10.0|^11.0
- spatie/laravel-package-tools: ^1.14
Requires (Dev)
- godruoyi/php-snowflake: ^3.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.20
- pestphp/pest-plugin-laravel: ^2.2
- spatie/laravel-ray: ^1.31
- tuupola/ksuid: ^2.1
This package is auto-updated.
Last update: 2024-09-08 13:37:54 UTC
README
Human Keys为您的Laravel模型提供使用UUID的替代方案。默认情况下,它生成ksuids,类似于Stripe用于其资源的方式。ksuids可读性强且可排序。
示例
pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR
用于Models/Post.php
usr_p6UEyCc8D8ecLijAI5zVwOTP3D0
用于Models/User.php
安装
您可以通过composer安装此包
composer require rawilk/human-keys
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="human-keys-config"
如果您使用的是KsuidGenerator
(默认),您需要安装tuupola/ksuid
包
composer require tuupola/ksuid
有关更多信息,请参阅使用不同的生成器。
您可以在此处查看默认配置:https://github.com/rawilk/human-keys/blob/main/config/human-keys.php
使用方法
要开始使用,请在您的模型上使用HasHumanKey
特质
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Rawilk\HumanKeys\Concerns\HasHumanKey; class Post extends Model { use HasHumanKey; }
使用不同的生成器
默认情况下,该包配置为使用KsuidGenerator
,但是您可以在配置文件中的generator
键下定义一个自定义生成器来使用。
以下将介绍如何使用此包包含的不同生成器,以及如何创建您自己的生成器。
KsuidGenerator
这是默认生成器,您需要安装tuupola/ksuid
包才能使用。此生成器将生成类似这样的内容:pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR
。
composer require tuupola/ksuid
SnowflakeGenerator
此生成器根据Twitter发布的Snowflake算法生成ID。您需要安装godruoyi/php-snowflake
包才能使用。此生成器将生成类似这样的内容:pos_451734027389370636
。
composer require godruoyi/php-snowflake
UuidGenerator
从v1.1.0
版本开始,您可以使用UuidGenerator
,它使用Laravel的Str::uuid()
助手生成ID。使用此生成器生成的ID将类似于:pos_b8a34e34553a41b885ae218ae81abd42
。此生成器的唯一要求是在配置文件中注册它;没有外部依赖项需要安装。
// config/human-keys.php 'generator' => \Rawilk\HumanKeys\Generators\UuidGenerator::class,
自定义生成器
您可以通过实现Rawilk\HumanKeys\Contracts\KeyGenerator
合约来定义自己的生成器。从生成器中,您可以根据应用程序的需求返回一个ID。
namespace App\Generators; use Rawilk\HumanKeys\Contracts\KeyGenerator; class CustomGenerator implements KeyGenerator { public function generate(?string $prefix = null): string { // Generate your ID here... } }
然后,在配置文件中,您可以指定您的生成器
'generator' => App\Generators\CustomGenerator::class,
重写键前缀
默认情况下,键是模型类名的前3个字符。您可以通过在模型上定义humanKeyPrefix
方法来重写此内容
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Rawilk\HumanKeys\Concerns\HasHumanKey; class Post extends Model { use HasHumanKey; public static function humanKeyPrefix() : string { // You should omit an underscore at the end of the prefix, as it will be added automatically // by the generator. return 'custom_prefix'; } }
用于其他列
默认情况下,HasHumanKey
特质将为模型的主键列生成一个ID。但这可能不是您想要的。在您的模型中,您可以重写humanKeys
方法并返回应该生成的列的列表。
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Rawilk\HumanKeys\Concerns\HasHumanKey; class Post extends Model { use HasHumanKey; public function humanKeys(): array { return ['human_key']; } }
现在将生成human_key
列,而不是主键。如果您已使用自增ID或使用UUID
或ULID
作为主键,则此功能非常有用。HasHumanKey
特质与Laravel的HasUuids
或HasUlids
特质完全兼容。
如果您真的需要,您甚至可以重写模型上的newHumanKey
方法以自定义生成方式生成自定义ID,但在大多数情况下,这通常不是必要的。
脚本
设置
为了方便,您可以使用设置脚本来轻松安装并用于本地开发。
./bin/setup.sh
格式化
尽管格式化是通过工作流程自动完成的,但您可以在提交前使用composer脚本来本地格式化PHP代码。
composer format
测试
composer test
变更日志
有关最近变更的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全性
请查阅我的安全策略了解如何报告安全漏洞。
替代方案
此包与oneduo的laravel-human-keys包非常相似,但我创建了自版本,因为我希望有更多关于生成哪些列ID的灵活性。
其他替代方案包括
致谢
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅许可文件。