spatie/laravel-prefixed-ids

为 Laravel 模型提供友好的前缀 ID

1.4.0 2024-03-06 16:03 UTC

This package is auto-updated.

Last update: 2024-09-06 17:26:33 UTC


README

Latest Version on Packagist run-tests Total Downloads

为 ID 添加前缀将帮助用户识别其类型。Stripe 默认这样做:客户 ID 前缀为 cus,生产环境中的密钥前缀为 sk_live_,测试环境中的密钥前缀为 sk_test_ 等等...

此包可以为 Eloquent 模型生成这样的友好前缀 ID。以下是一个这样的生成 ID 的示例。

user_fj39fj3lsmxlsl
test_token_dvklms109dls

该包可以检索给定前缀 ID 的模型。

// on a specific model
User::findByPrefixedId('user_fj39fj3lsmxlsl'); // returns a User model or `null`
User::findByPrefixedIdOrFail('user_fj39fj3lsmxlsl'); // returns a User model or throws `NoPrefixedModelFound`

// automatically determine the model of a given prefixed id
$user = PrefixedIds::getModelClass('user_fj39fj3lsmxlsl') // returns the right model for the id or `null`;

支持我们

我们投入了大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从家乡寄来明信片,说明您正在使用我们的哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在我们的 虚拟明信片墙上

安装

您可以通过 composer 安装此包

composer require spatie/laravel-prefixed-ids

准备您的模型

在每个需要前缀 ID 的模型上,您应该使用 Spatie\PrefixedIds\Models\Concerns\HasPrefixedId 特性。

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\PrefixedIds\Models\Concerns\HasPrefixedId;

class YourModel extends Model
{
    use HasPrefixedId;
}

准备数据库

对于每个需要前缀 ID 的模型,您需要编写一个迁移来为其底层表添加一个 prefixed_id 列。

如果您想使用另一个属性名,您应该发布配置文件(见下文),并将 prefixed_id_attribute_name 配置值设置为喜欢的属性名。

Schema::create('your_models_table', function (Blueprint $table) {
   $table->string('prefixed_id')->nullable()->unique();
});

注册带有前缀 ID 的模型

要注册您的模型,您应将所需的前缀和您的模型类名传递给 PrefixedIds::registerModels

Spatie\PrefixedIds\PrefixedIds::registerModels([
    'your_prefix_' => YourModel::class,
    'another_prefix' => AnotherModel::class,
]);

通常,您会将上面的代码放在服务提供器中。

发布配置文件

可选地,您可以通过以下方式发布配置文件:

php artisan vendor:publish --provider="Spatie\PrefixedIds\PrefixedIdsServiceProvider" --tag="prefixed-ids-config"

这是发布配置文件的内容

return [
    /*
     * The attribute name used to store prefixed ids on a model
     */
    'prefixed_id_attribute_name' => 'prefixed_id',
];

用法

当创建模型时,它将自动在 prefixed_id 属性中具有唯一的前缀 ID。

$model = YourModel::create();
$model->prefixed_id // returns a random id like `your_model_fekjlmsme39dmMS`

查找特定模型

您可以通过对模型调用 findByPrefixedId 来查找具有给定前缀的模型。

YourModel::findByPrefixedId('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel`
YourModel::findByPrefixedId('non-existing-id'); // returns null
YourModel::findByPrefixedIdOrFail('non-existing-id'); // throws `NoPrefixedModelFound`

跨模型查找

您可以通过对 Spatie\PrefixedIds\PrefixedIds 调用 find 来自动获取任何给定前缀 ID 的正确模型。

$yourModel = Spatie\PrefixedIds\PrefixedIds::find('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel` or `null`
$otherModel = Spatie\PrefixedIds\PrefixedIds::find('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or `null`
$otherModel = Spatie\PrefixedIds\PrefixedIds::findOrFail('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or throws `NoPrefixedModelFound`

自定义生成的唯一 ID

您可以使用函数 Spatie\PrefixedIds\PrefixedIds::generateUniqueIdUsing() 传入一个函数来生成唯一 ID。默认情况下,库将使用 Str::uuid() 来生成 ID。

// generate a unique Id with a set length
Spatie\PrefixedIds\PrefixedIds::generateUniqueIdUsing(function(){
    $length = 8;
    return substr(md5(uniqid(mt_rand(), true)), 0, $length);
});

在路由中使用前缀 ID

要使用前缀 ID 在路由中,您必须在模型中添加 getRouteKeyName 方法。它应返回包含前缀 ID 的属性名。

public function getRouteKeyName()
{
    return 'prefixed_id';
}

有了这个,一个定义为...

Route::get('/api/your-models/{yourModel}', YourModelController::class)`

... 的路由可以通过这样的 URL 来调用 /api/your-models/your_model_fekjlmsme39dmMS

您可以在Laravel 文档中找到更多关于路由模型绑定的信息。

测试

composer test

更新日志

有关最近更改的详细信息,请参阅更新日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞的详细信息,请查看我们的安全策略

致谢

本包灵感来源于excid3/prefixed_ids

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅许可文件