alihann/laravel-rockid
Laravel Rockid
Id obfuscation for Laravel using Optimus.
How to Install
-
composer require
composer require alihann/laravel-rockid -
in your config/app.php
'providers' => [ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, ... Alihann\LaravelRockid\RockidServiceProvider::class, ], -
and if you want to use facade
'aliases' => [ 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, ... 'Rockid' => Alihann\LaravelRockid\Facades\Rockid::class, ], -
publish the config file
php artisan vendor:publish -
generate numbers and add to the published config file. (i.e. config/rockid.php)
php artisan rockid:generate
Usage
you can use ObfuscatesId trait to get the obfuscated id of the model in your views.
use Illuminate\Database\Eloquent\Model;
use Alihann\LaravalRockId\ObfuscatesId;
class User extends Model {
use ObfuscatesId;
}
now you have getId method in your model to generate an obfuscated id.
<a href="user/{{ $user->getId() }}">Show user</a>
routes.php
Route::bind('user', function ($value) {
$id = Rockid::decode($value);
return \App\User::find($id);
});
Route::get('user/{user}', function ($user) {
return $user->getId();
});
or in RouteServiceProvider class
public function boot(Router $router)
{
parent::boot($router);
$router->bind('user', function ($value) {
$id = app('rockid')->decode($value);
return \App\User::find($id);
});
}