- Notifications
You must be signed in to change notification settings - Fork6
Hashidable uses hashids to obfuscate Laravel route ids.
License
kayandra/hashidable
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Hashidable useshashids to obfuscate Laravel route ids.
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
Import theHashidable
trait and add it to your model.
useKayandra\Hashidable\Hashidable;Class Userextends Model{use Hashidable;}
$user = User::find(1);$user->id;// 1$user->hashid;// 3RwQaeoOR1E7qjYyUser::find(1);User::findByHashId('3RwQaeoOR1E7qjYy');User::findByHashidOrFail('3RwQaeoOR1E7qjYy');
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.
publicfunctionshow(Request$request,User$user){return$user;// Works just fine}
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' =>'-',];
You can also extend the global configuration on a per-model basis. To do this, your model should implement theKayandra\Hashidable\HashidableConfigInterface
and define thehashidableConfig()
method on the model.
This method returns an array or subset of options similar to the global configuration.
publicfunctionhashidableConfig() {return ['prefix' =>'app']; }
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.
About
Hashidable uses hashids to obfuscate Laravel route ids.