Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Controllers  ›  Lesson

Route Groups

Route groups 12 minA route group applies shared settings such as middleware, a URI prefix, a name prefix or a controller to every route inside it, so each route states only what is unique to it.

Route Groups

Lendly's admin pages all start with /admin, all need a logged-in user, and all have names starting with admin.. Writing those three things on every admin route is noisy and easy to forget once. Forget the login check on one route and you have a security hole. A route group says it once for all of them.

The rule

Say the shared part once; every route inside inherits it.

A group is a set of settings followed by ->group() and a closure holding the routes:

Route::middleware('auth')
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {
        Route::get('/books', [AdminBookController::class, 'index'])->name('books');
        Route::get('/members', [AdminMemberController::class, 'index'])->name('members');
    });

What each setting does to the routes inside:

  • middleware('auth'): runs the middleware before every route, so guests are sent to log in.
  • prefix('admin'): adds /admin in front of every URI, so /books becomes /admin/books.
  • name('admin.'): glues admin. onto the front of every route name, so books becomes admin.books. The trailing dot is yours to include.
  • controller(...): lets routes name only the method.
Route::controller(LoanController::class)->group(function () {
    Route::get('/loans', 'index');
    Route::post('/loans', 'store');
});

Groups can be nested, and the settings stack up: prefixes join with /, names join end to end, and middleware lists are combined.

The shortcut

To predict a grouped route, build it outside-in: start from the outermost group, add each layer's prefix, name and middleware, then the route's own part last.

Resolving a nested admin routeStep 1 of 4
  1. Outer group

    middleware('auth')->prefix('admin')->name('admin.').

The trap is forgetting the dot in name('admin'). The names become adminbooks and adminmembers, and route('admin.books') fails.

Drills

Answer fast — trust the rule.

quick drill

Inside Route::prefix('shop')->group(...), what URI does Route::get('/cart', ...) get?

quick drill

A group uses name('admin') without a dot, and a route inside is named books. What is the full name?

quick drill

Why put all admin routes in one middleware('auth') group?

quick drill

Inside Route::controller(BookController::class)->group(...), how does a route name its action?

Remember

  • Say the shared part once; every route inside inherits it.
  • Build grouped routes outside-in: prefixes, names and middleware stack.
  • Trap: the name prefix needs its own trailing dot.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainAn admin area in one block

Lendly needs admin pages at /admin/books and /admin/members, named admin.books and admin.members, that only logged-in users can reach. Write a route group for them and explain what each part of the group adds to the routes inside.