oneduo / laravel-human-keys
为您的 Eloquent 模型提供可配置和可自定义的类似 Stripe 的密钥
v0.1.2
2023-02-04 19:15 UTC
Requires
- php: ^8.0
- godruoyi/php-snowflake: ^2.1
- illuminate/contracts: ^9.0|^10.0
- spatie/laravel-package-tools: ^1.13.0
- tuupola/ksuid: ^2.1
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-09-08 11:13:17 UTC
README
一个用于在 Laravel 模型中使用可读密钥的包。受 Stripe 的 ID 生成流程启发。
使您能够在模型中拥有可读和可排序的 KSUID 密钥。
示例
pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR
用于Models/Post.php
usr_p6UEyCc8D8ecLijAI5zVwOTP3D0
用于Models/User.php
目录
安装
您可以通过 composer 安装此包
composer require oneduo/laravel-human-keys
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="human-keys-config"
这是发布配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Generator |-------------------------------------------------------------------------- | | Used to define the generator to use for generating model keys. | | Supported: | - ksuid (abc_p6UEyCc8D8ecLijAI5zVwOTP3D0) | - snowflake (abc_1537200202186752) | | Default: ksuid | | Note: You may define your own generator by implementing the contract | Oneduo\LaravelHumanKeys\Contracts\Generator and passing | the class name to the generator config option. | | See the example below: | 'generator' => \App\Services\MyGenerator::class */ 'generator' => 'ksuid', ];
使用方法
要开始使用,请在您的模型中使用 HasHumanKey
特性
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Oneduo\LaravelHumanKeys\Concerns\HasHumanKey; class Post extends Model { use HasHumanKey; }
当使用 ksuid
生成器时,生成的密钥将类似于:pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR
当使用 snowflake
生成器时,生成的密钥将类似于:pos_451734027389370636
覆盖密钥前缀
您可以通过实现以下方法为每个模型设置自己的密钥前缀
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Oneduo\LaravelHumanKeys\Concerns\HasHumanKey; class Post extends Model { use HasHumanKey; public static function getKeyPrefix() : string { // prefix without _ underscore as it gets added by the generator return 'post_prefix' } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG
贡献
有关详细信息,请参阅 CONTRIBUTING
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件