Enables automatic Eloquent model ID obfuscation in routes using Optimus.
-
In the
require
key ofcomposer.json
file add the following"propaganistas/laravel-fakeid": "~1.0"
-
Run the Composer update command
composer update
-
In your app config, add the Service Provider to the end of the
$providers
arrayLaravel 5
'providers' => [ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, ... Propaganistas\LaravelFakeId\FakeIdServiceProvider::class, ],
-
Run the following artisan command to auto-initialize the package's settings
php artisan fakeid:setup
First of all, make sure the model is bound to Laravel's Router, e.g. by inserting this on top of the routes.php
file:
Route::model('mymodel', 'App\MyModel');
This way you can reference a placeholder in your routes (edit/{mymodel}
)
Next, simply import Propaganistas\LaravelFakeId\FakeIdTrait
into your model:
use Illuminate\Database\Eloquent\Model;
use Propaganistas\LaravalFakeId\FakeIdTrait;
class MyModel extends Model {
use FakeIdTrait;
}
All routes generated for this model will now automatically contain obfuscated IDs and incoming requests to {mymodel}
routes will be handled correctly.
This package features a slightly modified version of Laravel's built-in Illuminate\Routing\Router
class and injects it into the IoC container. If you are using a custom Router
of your own, you can publish the config file to disable FakeId's Router
. Please note that in this case you need to decode incoming requests containing obfuscated IDs yourself.
Why didn't you implement Hashids instead of Optimus?
Simple: speed! Optimus is based on Knuth's multiplicative hashing method and proves to be quite faster than Hashids. It's even mentioned on Hashids' own website.