coderscantina / hashidable
适用于Laravel模型中使用hashids的自适应桥梁。
v1.0.2
2023-05-23 15:59 UTC
Requires
- php: ~7.3||^8.0
- illuminate/support: ^8.0||^9.0||^10.0
- vinkla/hashids: ~9.1||~10.0||^11.0
Requires (Dev)
- graham-campbell/testbench: ^5.4||^6.0
- illuminate/database: ^8.0||^9.0||^10.0
- mockery/mockery: ^1.3
- phpunit/phpunit: ^9.3
README
为Laravel模型使用laravel-hashids的自适应桥梁。
特性
- Hashid路由模型绑定
- 每个模型使用独立的盐
- 每个模型可选的独立配置
- 编码、解码和按hashid查找的辅助方法
🏗 安装
使用以下命令通过composer安装此包
composer require coderscantina/hashidable
⚙️ 使用方法
将Hashidable特性添加到您的模型中
use CodersCantina\Hashidable; class Phone extends Model { use Hashidable; }
在资源中公开hashid
class PhoneResource extends JsonResource { /** * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { return [ 'id' => $this->getRouteKey(), ]; } }
在控制器中通过hashid解析模型
/** * @param \App\Models\Phone $phone * @return \Illuminate\Http\Response */ public function show(Phone $phone) { return new PhoneResource($phone); }
处理hashIds的静态方法
Foo::encodeHashId(1); Foo::decodeHashId('A3'); Foo::findByHashId('A3');
使用类似App\User::class
的模型覆盖配置
# config/hashids.php 'connections' => [ 'main' => [ 'salt' => env('HASHIDS_SALT'), 'length' => 8, 'alphabet' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ], \App\User::class => [ 'salt' => env('HASHIDS_SALT'), 'length' => 5, 'alphabet' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ], ],
更多信息请参阅路由模型绑定