markwalet/laravel-hashed-route

A Laravel package that replaces the default route model binding for a safer version.

v2.8.0 2023-07-06 18:13 UTC

README

Laravel hashed route

MIT Licensed Latest Stable Version Build status Coverage StyleCI Total Downloads

A Laravel package that replaces the default route model binding for a safer version.

Installation

You can install this package with composer:

composer require markwalet/laravel-hashed-route

Laravel 5.5 uses Package auto-discovery, so you don't have to register the service provider. If you want to register the service provider manually, add the following line to your config/app.php file:

MarkWalet\LaravelHashedRoute\HashedRouteServiceProvider::class

Usage

When you want to hash the routes for a given model. The only thing you have to is is adding the HasHashedRouteKey trait:

use MarkWalet\LaravelHashedRoute\Concerns\HasHashedRouteKey;

class TestModel extends Model
{
    use HasHashedRouteKey;
    
    //...
}

After that you can use the model like you normally would. Because the trait overrides the resolveRouteBinding() and getRouteKey() methods, no extra changes are required to your code.

You do have to change your code when you are building your urls by manually getting the $model->id property from your model. Then you will have to change those calls to $model->getRouteKey().

Configuration

The default configuration is defined in hashed-route.php. If you want to edit this file you can copy it to your config folder by using the following command:

php artisan vendor:publish --provider="MarkWalet\LaravelHashedRoute\HashedRouteServiceProvider"

In this file you can configure different codecs for the encoding and decoding of keys, as well as setting a default configuration.

You can override this configuration by setting the codec property on your model.

The supported codec drivers are: null, hashids, optimus & base64. Use the null driver if you want to disable route key hashing.