friendsofhyperf / model-hashids
为 Hyperf 模型提供 hashids。
资助包维护!
huangdijia
hdj.me/sponsors
Requires
- php: >=8.1
- hashids/hashids: ^4.1 || ^5.0
- hyperf/config: ~3.1.0
- hyperf/database: ~3.1.0
- hyperf/stringable: ~3.1.0
This package is auto-updated.
Last update: 2024-09-25 00:30:20 UTC
README
在 URL 和列表项中使用 hashids 代替整数 ID 可以更加吸引人且巧妙。更多信息请访问 hashids.org。
此功能通过在运行时对 Hyperf 模型进行编码/解码,而不是在数据库中持久化它们,为 Hyperf 模型添加 hashids。因此,不需要另一个数据库列,并且通过在查询中使用主键来提高性能。
特性包括
- 为模型生成 hashids
- 解析 hashids 到模型
- 可以为每个模型自定义 hashid 设置
- 使用 hashids 绑定路由(可选)
安装
使用 Composer 安装包
composer require friendsofhyperf/model-hashids
此外,将供应商配置文件发布到您的应用程序中(对于依赖项是必要的)
php bin/hyperf.php vendor:publish friendsofhyperf/model-hashids
设置
通过使用 HasHashid
特性提供基本功能,然后可以使用 HashidRouting
添加路由绑定。
use Hyperf\Database\Model\Model; use FriendsOfHyperf\ModelHashids\Concerns\HasHashid; use FriendsOfHyperf\ModelHashids\Concerns\HashidRouting; Class Item extends Model { use HasHashid, HashidRouting; }
自定义 hashid 设置
可以通过重写 getHashidsConnection()
来为每个模型自定义 hashids 设置。它必须返回 config/autoload/hashids.php
中连接的名称。
使用方法
基础
// Generating the model hashid based on its key $item->hashid(); // Equivalent to the above but with the attribute style $item->hashid; // Finding a model based on the provided hashid or // returning null on failure Item::findByHashid($hashid); // Finding a model based on the provided hashid or // throwing a ModelNotFoundException on failure Item::findByHashidOrFail($hashid); // Decoding a hashid to its equivalent id $item->hashidToId($hashid); // Encoding an id to its equivalent hashid $item->idToHashid($id); // Getting the name of the hashid connection $item->getHashidsConnection();
将 hashid 添加到序列化的模型中
将其设置为默认值
use Hyperf\Database\Model\Model; use FriendsOfHyperf\ModelHashids\Concerns\HasHashid; class Item extends Model { use HasHashid; /** * The accessors to append to the model's array form. * * @var array */ protected $appends = ['hashid']; }
或指定它
return $item->append('hashid')->toJson();
隐式路由绑定
如果您想使用模型的 hashid 值来解决模型的隐式路由绑定,您可以在模型中使用 HashidRouting
。
use Hyperf\Database\Model\Model; use FriendsOfHyperf\ModelHashids\Concerns\HasHashid; use FriendsOfHyperf\ModelHashids\Concerns\HashidRouting; class Item extends Model { use HasHashid, HashidRouting; }
它覆盖了 getRouteKeyName()
、getRouteKey()
和 resolveRouteBindingQuery()
以使用 hashids 作为路由键。
它支持 Laravel 的功能来自定义特定路由的键。
Route::get('/items/{item:slug}', function (Item $item) { return $item; });
自定义默认路由键名称
如果您想默认使用另一个字段解决隐式路由绑定,可以重写 getRouteKeyName()
来返回字段名称到解析过程中,并重写 getRouteKey()
来返回其值在您的链接中。
use Hyperf\Database\Model\Model; use FriendsOfHyperf\ModelHashids\Concerns\HasHashid; use FriendsOfHyperf\ModelHashids\Concerns\HashidRouting; class Item extends Model { use HasHashid, HashidRouting; public function getRouteKeyName() { return 'slug'; } public function getRouteKey() { return $this->slug; } }
您仍然可以指定特定路由的 hashid。
Route::get('/items/{item:hashid}', function (Item $item) { return $item; });
支持其他 Laravel 的隐式路由绑定功能
当使用 HashidRouting
时,您仍然可以使用软删除和子路由绑定。
Route::get('/items/{item}', function (Item $item) { return $item; })->withTrashed(); Route::get('/user/{user}/items/{item}', function (User $user, Item $item) { return $item; })->scopeBindings();