3 Ways to group routes in laravel

Today, I would like to share about 3 ways to group routes in laravel. Let’s take a look.

1. Route::resource

We can create a resource controller and group routes for CRUD actions of that controller as follow.

Route::resource('books', BookController::class);

2. Route groups within another route group

We can groups routes within another route groups like nested ones.

Route::middleware('auth')->group(function() {
 
    Route::middleware('admin')->prefix('admin')->group(function() {
    	Route::get(...) 
    });
 
    Route::middleware('member')->prefix('member')->group(function() {
    	Route::get(...) 
    });
});

3. Route::controller() in Laravel 9

In laravel 9, we can group the routes without repeating controller name for the same controller.

Route::controller(PostController::class)->group(function() {
    Route::get('posts', 'getPosts');
    Route::put('posts/update', 'updatePost');
    Route::delete('posts/delete/{id}', 'deletePost');
});

This is all for now. Hope you enjoy that.

By Asahi



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム