oneduo/laravel-human-keys

为您的 Eloquent 模型提供可配置和可自定义的类似 Stripe 的密钥


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

一个用于在 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)。有关更多信息,请参阅 许可证文件