Laravel 9 Route Group Controller Example

10-Apr-2023

.

Admin

Laravel 9 Route Group Controller Example

Today, I would like to show you Assigning a controller to a route group. This article will give you simple example of Grouping routes by controllers in Laravel. I explained simply step by step Laravel 9 route group with controller. This article goes in detailed on How To Use Controller Route Groups in Laravel.

you may use the controller method to define the common controller for all of the routes within the group. Then, when defining the routes, you only need to provide the controller method that they invoke:

Example 1:


use App\Http\Controllers\UserController;

Route::controller(UserController::class)->group(function () {

Route::get('/users/{id}', 'show');

Route::post('/users', 'store');

});

Example 2:

use App\Http\Controllers\PostController;

Route::controller(PostController::class)->group(function () {

Route::get('/posts/{id}', 'show');

Route::post('/posts', 'store');

});

It will help you...

#Laravel 9