Search code examples
phplaravelroutesgrouping

how group apiResource route and other routes together?


I am using apiResource and other routes. I grouped them like below:

Route::group(['prefix' => 'posts'], function () {
    Route::group(['prefix' => '/{post}'], function () {
         Route::put('lablabla', [PostController::class, 'lablabla']);
    });
    Route::apiResource('/', PostController::class, [
        'names' => [
            'store' => 'create_post',
            'update' => 'edit_post',
        ]
    ]);
});

all apiResource routes except index and store do not work! How should I group routes?


Solution

  • Your syntax for routing is wrong,

    Notes

    1. You will provide a uri for the apiResource (plural)
    • eg. Route::apiResource('posts', PostController::class);
    1. Your name of resource route is wrong
    1. No need of repeating Route::group, you can just write your routes like this

       Route::prefix('posts')->group(function () {
           Route::put('lablabla', [PostController::class, 'lablabla']); 
       });
      
       Route::apiResource('posts', PostController::class)->names([
           'store' => 'create_post',
           'update' => 'edit_post',
       ]);