Laravel 9 Controller Route Grouping

A new feature in laravel 9 is controller route grouping. This enable you to group routes according to the controller used.

Route::controller(PostController::class)->group(function () {
    Route::get('/post', 'index');
    Route::get('/post/{post}', 'show');
    Route::post('/post', 'store');
});

Compareing this to Laravel 8 where you are unable to group controller routes:

Route::get('/post', [PostController::class, 'index']);
Route::get('/post/{post}', [PostController::class, 'show']);
Route::post('/post', [PostController::class, 'store']);

You will agree that it’s much cleaner. Also running the command php artisan route:list, this has been given a face lift:

GET|HEAD   / ........................
GET|HEAD   post ..................... PostController@index
POST       post ..................... PostController@store
GET|HEAD   post/{post} .............. PostController@show

Leave a Reply

Your email address will not be published.