kayandra/hashidable
Composer 安装命令:
composer require kayandra/hashidable
包简介
Auto Hashids for Laravel Models and Routes
README 文档
README
Hashidable uses hashids to obfuscate Laravel route ids.
Installation
Note: This package is built to work with Laravel versions greater than 7. It may work in older version, but this has not been tested.
composer require kayandra/hashidable
Setup
Import the Hashidable trait and add it to your model.
use Kayandra\Hashidable\Hashidable; Class User extends Model { use Hashidable; }
Usage
$user = User::find(1); $user->id; // 1 $user->hashid; // 3RwQaeoOR1E7qjYy User::find(1); User::findByHashId('3RwQaeoOR1E7qjYy'); User::findByHashidOrFail('3RwQaeoOR1E7qjYy');
Route Model Binding
Assuming we have a route resource defined as follows:
Route::apiResource('users', UserController::class);
This package does not affect route model bindings, the only difference is, instead of placing the id in the generated route, it uses the hashid instead.
So, route('users.show', $user) returns /users/3RwQaeoOR1E7qjYy;
When you define your controller that auto-resolves a model in the parameters, it will work as always.
public function show(Request $request, User $user) { return $user; // Works just fine }
Configuring
First, publish the config file using:
php artisan vendor:publish --tag=hashidable.config
The available configuration options are:
return [ /** * Length of the generated hashid. */ 'length' => 16, /** * Character set used to generate the hashids. */ 'charset' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', /** * Prefix attached to the generated hash. */ 'prefix' => '', /** * Suffix attached to the generated hash. */ 'suffix' => '', /** * If a prefix of suffix is defined, we use this as a separator * between the prefix/suffix. */ 'separator' => '-', ];
Per-Model Configuration
You can also extend the global configuration on a per-model basis. To do this, your model should implement the Kayandra\Hashidable\HashidableConfigInterface and define the hashidableConfig() method on the model.
This method returns an array or subset of options similar to the global configuration.
public function hashidableConfig() { return ['prefix' => 'app']; }
FAQs
Where are the generated hashes stored?
Hashidable does not touch the database to store any sort of metadata. What it does instead is use an internal encoder/decoder to dynamically calculate the hashes.
License
kayandra/hashidable 适用场景与选型建议
kayandra/hashidable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.11k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2020 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kayandra/hashidable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kayandra/hashidable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-25