Routing Resources in Laravel 8

The questioner has a route that requires the locale to be in the prefix. They have defined their routes in blade, and while they are able to access the index route, they are unable to access the update route. They are wondering how to properly define the update route to return the correct URL. The solution to their issue is to pass their parameters in the correct order. The questioner also mentions an error they encountered after upgrading from a previous version, where routes with parameters in the middle of the path were not working on the server but were working on the local environment. The solution to this issue was to move the parameter to the end of the route, which resolved the issue on both the server and local environment.


Question:

Utilizing

apiResource

along with additional paths, I have organized them in the following manner:

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',
        ]
    ]);
});

How do I group routes when all the MSDT APIs, except for

index

and

store

, are not functional?


Solution 1:

Your syntax for routing is wrong,


Notes

  1. Please specify a URI for the API resource in its plural form.
  • An example of a route that utilizes the apiResource method is the following: Route::apiResource(‘posts’, PostController::class).
  1. Your resource route has been named incorrectly.
  • Access the documentation on restful naming resource routes by visiting https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes.

  • <p>
    it should be
    </p>

     Route::apiResource('posts', PostController::class)->names([
          'store' => 'create_post',
          'update' => 'edit_post',
     ]);
    
  1. There’s no need to repeat the “Route::group” statement, as you can simply write your routes in this manner.

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


Solution 2:


The syntax you used is wrong, but there’s a names method available. For more information, please refer to the documentation on RESTful naming resource routes at https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes.

Frequently Asked Questions