Search code examples
laravelcomposer-phpupgradelaravel-8

route model binding doesn't work anymore with api package devolpment in in Laravel 8.x


I created an api which is delivered with package development composer. in Laravel 7 it was possible to add the route model binding with:

Route::group(['namespace' => 'Acme\Package\app\Http\Controllers\Api'], function () {

// fomer possibility:
Route::apiResource('api/comments/', 'CommentController')->middleware('bindings');});

In Laravel 8 that's not possible anymore. I've tried almost everything the last days, but either the route-model-binding is not working, or the class can't be found:

Target class [bindings] does not exist.

I really hope someone can post an answer to the problem, or a hint or anything useful.

many thanks in advance

EDIT:

Thanks for the answers, including the middleware in the Route::group like mentioned:

Route::group(['namespace' => 'Acme\Package\app\Http\Controllers\Api', 'middleware' => 'Illuminate\Routing\Middleware\SubstituteBindings']

did it.


Solution

  • In Laravel 8 the alias for this middleware has been removed. You can either use it by its full class name

    Illuminate\Routing\Middleware\SubstituteBindings

    or add the alias back in to your app/Http/Kernels.php in $routeMiddleware as follows:

    protected $routeMiddleware = [
     'auth'       => Authenticate::class,
     'bindings'   => Illuminate\Routing\Middleware\SubstituteBindings, 
    /*...*/