Currently, people can access a resource, let's call it Entity, via the following route:
Route::get('/entity/{entity}', 'EntityController@view');
I can then type hint \App\Entity on my controller to automatically retrieve the Entity requested in the URL.
Now let's say I want an authenticated user to be able to edit only their own Entity, so I have the following route:
Route::middleware('auth')->group(function () {
Route::get('/{id}/edit', 'EntityController@edit');
}
Then my controller looks like this:
public function edit($id)
{
$entity = Auth::user()->entities()->where('id', $id)->firstOrFail();
...
}
However, I need that first line of code in every controller method that needs to check the Entity belongs to the currently authenticated user (i.e. update, delete, etc).
I discovered I could create custom route model binding like so:
Route::bind('entity_user', function($value)
{
return Auth::user()->entities()->where('id', $value)->firstOrFail();
});
Then my route would look like this:
Route::middleware('auth')->group(function () {
Route::get('/{entity_user}/edit', 'EntityController@edit');
}
And my controller methods would be simplified:
public function edit(Entity $entity)
{
...
}
Whilst this is a lot cleaner, I'm wondering if it's more difficult to understand from glancing at the code. Furthermore, if anyone ever edited the routes and changed entity_user to entity then it would have disastrous consequences, because all users would suddenly be able to edit Entities which don't belong to them.
So I'm wondering which is the "best" way?