Lesson 9 of 50 – Laravel Route Groups and Middleware
18%

Laravel Route Groups and Middleware

Route groups allow you to organize multiple routes and apply shared settings such as URL prefixes, name prefixes, and middleware. They help keep Laravel applications organized.

1. What Are Route Groups?

A route group combines related routes under a common configuration.

Example:

use Illuminate\Support\Facades\Route;

Route::prefix('admin')->group(function () {

    Route::get('/dashboard', function () {
        return 'Admin Dashboard';
    });

    Route::get('/students', function () {
        return 'Manage Students';
    });

});

The routes become:

  • /admin/dashboard
  • /admin/students

The prefix() method adds a common URL prefix to every route inside the group.

2. Route Groups with URL Prefixes

Use prefix() when several routes should start with the same URL segment.

Route::prefix('student')->group(function () {

    Route::get('/profile', function () {
        return 'Student Profile';
    });

    Route::get('/courses', function () {
        return 'Student Courses';
    });

    Route::get('/fees', function () {
        return 'Student Fees';
    });

});

The generated route paths are:

  • /student/profile
  • /student/courses
  • /student/fees

This is useful for student panels, admin panels, and other sections of a website.

3. Route Groups with Name Prefixes

Use name() to give routes a shared name prefix.

Route::name('admin.')->group(function () {

    Route::get('/dashboard', function () {
        return 'Admin Dashboard';
    })->name('dashboard');

    Route::get('/students', function () {
        return 'Manage Students';
    })->name('students');

});

The route names become:

  • admin.dashboard
  • admin.students

Generate a URL using the route name:

$url = route('admin.dashboard');

Notice that name('admin.') includes a trailing dot. Laravel does not add this dot automatically.

4. Combining URL and Name Prefixes

You can combine prefix() and name() in the same group.

Route::prefix('admin')
    ->name('admin.')
    ->group(function () {

        Route::get('/dashboard', function () {
            return 'Admin Dashboard';
        })->name('dashboard');

        Route::get('/students', function () {
            return 'Manage Students';
        })->name('students');

    });

Result:

URL Path Route Name
/admin/dashboard admin.dashboard
/admin/students admin.students

URL prefixes affect paths, while name prefixes affect the names used by Laravel's routing helpers.

5. Route Groups with Middleware

Middleware can inspect or filter incoming requests before they reach a route.

For example, the built-in auth middleware generally requires a user to be authenticated.

Route::middleware('auth')->group(function () {

    Route::get('/dashboard', function () {
        return 'Protected Dashboard';
    });

    Route::get('/profile', function () {
        return 'Protected Profile';
    });

});

Both routes are protected by the auth middleware.

If the user is not authenticated, Laravel's authentication configuration determines how the request is handled. In a typical browser application, the user may be redirected to a login page.

Adding middleware does not create a login system by itself; authentication must also be configured.

6. Combine Prefixes and Middleware

You can protect an entire section of your website and give it a common URL prefix.

Route::prefix('admin')
    ->middleware('auth')
    ->group(function () {

        Route::get('/dashboard', function () {
            return 'Admin Dashboard';
        })->name('admin.dashboard');

        Route::get('/students', function () {
            return 'Manage Students';
        })->name('admin.students');

    });

These routes use the paths:

  • /admin/dashboard
  • /admin/students

Both routes require authentication. However, authentication alone does not guarantee that a logged-in user is an administrator.

For administrator-only pages, add an appropriate authorization check or admin-specific middleware.

7. Route Groups with Controllers

Route groups can direct requests to controller methods instead of using route closures.

Create a controller:

php artisan make:controller AdminController

Add methods inside app/Http/Controllers/AdminController.php:

public function dashboard()
{
    return 'Admin Dashboard';
}

public function students()
{
    return 'Manage Students';
}

Define the routes:

use App\Http\Controllers\AdminController;

Route::prefix('admin')
    ->name('admin.')
    ->middleware('auth')
    ->controller(AdminController::class)
    ->group(function () {

        Route::get('/dashboard', 'dashboard')->name('dashboard');

        Route::get('/students', 'students')->name('students');

    });

The controller group avoids repeating the controller class for every route. The methods must exist in the controller.

8. Nested Route Groups and Route List

Route groups can be nested to organize larger applications.

Route::prefix('admin')->name('admin.')->group(function () {

    Route::prefix('students')->name('students.')->group(function () {

        Route::get('/', function () {
            return 'All Students';
        })->name('index');

        Route::get('/create', function () {
            return 'Create Student';
        })->name('create');

    });

});

The resulting route names are:

  • admin.students.index
  • admin.students.create

Their URL paths are /admin/students and /admin/students/create.

Inspect registered routes:

php artisan route:list

You can inspect the registered URLs, route names, and middleware in the command output.

9. Practical Assignment

Create the following route groups in routes/web.php.

  1. Create an admin group with the URL prefix admin.
  2. Add dashboard and students routes inside the group.
  3. Apply the route name prefix admin..
  4. Generate the URL for admin.dashboard.
  5. Protect a group of routes using auth middleware.
  6. Create a controller and group its routes using controller().
  7. Create a nested group for student management.
  8. Apply authorization checks to administrator-only routes.
  9. Run php artisan route:list and inspect your routes.

Remember: a URL prefix organizes paths, a name prefix organizes route names, and middleware controls how requests are handled.


🧠 Quick Quiz

1. Which method adds a URL prefix to a route group?




2. Which method applies middleware to a route group?




3. What does name('admin.') do in a route group?