lingxi/hashids
Composer 安装命令:
composer require lingxi/hashids
包简介
A Hashids bridge for Laravel
README 文档
README
安装
composer require lingxi/hashids
添加到 app.php
Lingxi\Hashids\HashidsServiceProvider::class
'Hashids' => Lingxi\Hashids\Facades\Hashids::class
Config
php artisan vendor:publish --provider='Lingxi\Hashids\HashidsServiceProvider'
<?php return [ 'default' => 'main', 'middleware' => [ 'open' => true, // 路由中需要被 decode 的 id 'route_parameters' => [ // ], // 请求参数需要被 decode 的 id 'request_parameters' => [ // ] ], // 开启严格模式之后,解密 id 错误会抛出异常 'strict' => [ 'enable' => true, 'default' => 0, ], 'connections' => [ 'main' => [ 'prefix' => '', 'salt' => 'your-salt-string', 'length' => 'your-length-integer', 'alphabet' => 'your-alphabet-string', ], ], ];
Middleware
添加中间件
如果是全局中间件,那么对 route 参数是无法自动 decode 的
\Lingxi\Hashids\Middleware\DecodePublicIdMiddleware::class,
在 config/hashids.php 中去配置需要解密的路由参数以及请求参数
Model
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Lingxi\Hashids\ModelTraits\PublicId; class Post extends Model { use PublicId; public function comments() { return $this->morphMany(\App\Comment::class, 'commentable'); } }
Then, get public id.
Post::first()->public_id
Debug
php artisan id:en 1 --uri=p
php artisan id:de sdfghjkla;sdjhasjhdfgahjsdjasd
统计信息
- 总下载量: 3.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-20