1 year ago

Route::bind has no affect on Resource Routes

Posted 1 year ago by deanlaw

I have setup my site to utilize HashIds by using a Route:bind statement in my RouteServiceProvider file which decodes an id variable if defined in a route.

Route::bind('id', function ($id) {
    $decoded_id = Hashids::decode($id)[0] ?? $id;
    return $decoded_id;

This works great when I define the routes in my web routes file individually, but is ignored if I am using a resource route.

This works:

Route::prefix('users')->group(function () {
    Route::get('/', '[email protected]');
    Route::get('create', '[email protected]');
    Route::post('/', '[email protected]');
    Route::get('{id}', '[email protected]');
    Route::get('{id}/edit', '[email protected]');
    Route::put('{id}', '[email protected]');
    Route::delete('{id}', '[email protected]');

This does not:

Route::resource('users', 'UserController');

With the resource route, if I try to hit "/users/someHashedId/edit", I get a 404 error. But if I individually create the edit route as above, the id is correctly decoded and the edit form is displayed properly. Is there any way to specify that the app should use my bind first before trying to generate the user passed into the resource route?

  • Laravel 5.5.34
  • PHP 7.1.8
  • OSX 10.11.6

Please sign in or create an account to participate in this conversation.