veelasky/laravel-hashid

Laravel Eloquent ORM 的 HashId 实现

v3.1.4 2023-10-03 07:32 UTC

README

Test Codacy Badge codecov Latest Stable Version StyleCI Total Downloads Dependents License

为您的 eloquent 模型提供自动化的 HashId 生成器。

版本兼容性

安装

composer require veelasky/laravel-hashid

使用 Laravel 包自动发现功能,此功能会自动将此包添加到您的 Laravel 应用中。

TLDR

简单地将 HashableId 特性添加到您打算与 HashId 一起使用的任何 eloquent 模型上。

示例

use Illuminate\Database\Eloquent\Model;
use Veelasky\LaravelHashId\Eloquent\HashableId;

class User extends Model {
    use HashableId;
    ...
}

使用方法

与 Eloquent 模型一起使用

$user = User::find(1);     // instance of user.
$user->hash;               // generate HashId.

// Database operation

// get user by hashed id.
$user = User::byHash($hash);

// get user by hashed id, and throw ModelNotFoundException if not present.
$user = User::byHashOrFail($hash);

// get hashed id from the primary key.
User::idToHash($id);

// get ID from hashed string.
User::hashToId($hash);

 // query scope with `byHash` method.
User::query()->byHash($hash);

默认情况下,所有哈希计算将在运行时进行,但有时您可能希望将哈希 id 持久化到数据库中。

注意:当使用持久化模型时,所有数据库查询都将检查表本身,除了:$model->hash 将始终在运行时计算。

class User extends Model {
    use HashableId;

    // add this property to your model if you want to persist to the database.
    protected $shouldHashPersist = true;

    // by default, the persisted value will be stored in `hashid` column
    // override column name to your desired name.
    protected $hashColumnName = 'hashid';
    ...
}

盐是根据您的应用程序密钥和 hash_alphabet 自动生成的。如果您需要在不同的项目之间使用相同的盐,可以设置 HASHID_SALT 环境变量。

路由绑定

当使用 HashableId 特性时,基础的 getRouteKey()resolveRouteBinding() 方法将被重写以使用 HashId 作为路由键。

use App\Models\User;

class UserController extends Controller
{
    /**
     * Route /users/{user}
     * Ex: GET /users/k1jTdv6l
     */
    public function show(User $user)
    {
        ...
    }
}

深入探讨

此包使用存储库模式来存储所有 HashId\HashId 类的实例化实现,这是为了在每个使用 HashableId 特性的 eloquent 模型上获得不同的哈希结果。

// using facade.
HashId::hashToId($hash, User::class)      // same as User::hashToId($hash);
HashId::idToHash($id, User::class)        // same as User::idToHash($hash);

// HashId facade class is an implementation of \Veelasky\Laravel\HashId\Repository

但是,您可以选择不使用任何 eloquent 模型或实现自己的逻辑到存储库。

HashId::make($key, $salt);              // will return \HashId\HashId class.

// once you instantiated the object, you can retrieve it on your next operation
HashId::get($key);

如果您使用的是单表继承模型,并且希望在所有继承的模型中具有相同的计算哈希,请使用 $hashKey 属性,这将确保在所有继承的模型中计算结果保持不变。

class User extends Model {
    protected $hashKey = 'somethingUnique';
}

class Customer extends User {

}

$customer = Customer::find(1);
$user = User::find(1);

$user->hash; // will be equal to $customer->hash

您也可以使用环境变量 HASHID_LENGTHHASHID_ALPHABET 分别指定哈希 Id 的长度和字符,或者使用以下命令发布配置文件

php artisan vendor:publish --tag=laravel-hashid-config

附加:验证规则

您还可以将其用作验证规则,只需将此规则添加到您的验证器中。

use App\Models\User;
use Veelasky\LaravelHashId\Rules\ExistsByHash;

...
Validator::make([
    'id' => $hashedId
], [
    'id' => ['required', new ExistsByHash(User::class)],
]);
...

许可证

MIT 许可证