iridiumintel/laravel-hashable-routes
最新稳定版本:v0.1.1
Composer 安装命令:
composer require iridiumintel/laravel-hashable-routes
包简介
A Laravel package that provides short, secure, and per-model configurable hashed route keys instead of incremental IDs.
README 文档
README
A Laravel package that replaces incremental IDs with short, secure, and per-model configurable hash IDs.
Built on top of hashids/hashids.
🚀 Features
- Generate short, obfuscated IDs for your models (
abc123instead of42). - Per-model configuration (custom salt & length).
- Optional automatic route key override (so
/users/abc123instead of/users/42). - Helpers:
encodeId(),decodeHash(),findByHashOrNull(). - Artisan commands for direct encode/decode in terminal.
- Plug-and-play with Laravel (
composer require).
📦 Installation
composer require iridiumintel/laravel-hashable-routes
Publish the config file:
php artisan vendor:publish --tag=config
⚙️ Configuration
config/hashable.php
return [
'default' => [
'salt' => env('HASHABLE_SALT', env('APP_KEY')),
'length' => env('HASHABLE_LENGTH', 10),
'override_route_key' => env('HASHABLE_ROUTE_KEY', true),
'strict_binding' => env('HASHABLE_STRICT', true),
],
'models' => [
// Example: per-model override
App\Models\User::class => [
'salt' => env('HASHABLE_USER_SALT', env('APP_KEY')),
'length' => 8,
'override_route_key' => true,
'strict_binding' => true,
],
],
];
🔑 Usage
Add the trait to your Eloquent model:
use IridiumIntel\Hashable\Hashable;
class User extends Model
{
use Hashable;
}
Now:
$user = User::find(42);
echo $user->hash_id;
// e.g. "gB9xL0Qjz"
$found = User::findByHash("gB9xL0Qjz");
// returns the same user
$maybe = User::findByHashOrNull("invalid");
// returns null instead of throwing
Route binding
If override_route_key is enabled (default):
Route::get('/users/{user}', fn(User $user) => $user);
// /users/gB9xL0Qjz → User#42
If disabled, routes continue using the standard id, but you can still use findByHash() manually.
🧪 Artisan Commands
Encode an ID:
php artisan hashable:encode "App\Models\User" 42
# Output: Hash for App\Models\User #42: gB9xL0Qjz
Decode a hash:
php artisan hashable:decode "App\Models\User" gB9xL0Qjz
# Output: Decoded ID for App\Models\User (gB9xL0Qjz): 42
📜 License
The MIT License (MIT).
See LICENSE for details.
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-27