Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Hashidable uses hashids to obfuscate Laravel route ids.

License

NotificationsYou must be signed in to change notification settings

kayandra/hashidable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hashidable useshashids 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 theHashidable trait and add it to your model.

useKayandra\Hashidable\Hashidable;Class Userextends Model{use Hashidable;}

Usage

$user = User::find(1);$user->id;// 1$user->hashid;// 3RwQaeoOR1E7qjYyUser::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.

publicfunctionshow(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 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'];    }

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

MIT

About

Hashidable uses hashids to obfuscate Laravel route ids.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp