Share this page
Back

Laravel Route Grouping with Middleware

Grouping Laravel routes with middleware enhances code structure and access control. Follow these steps:

Step 1: Open web.php
Navigate to routes/web.php in your Laravel project.

Step 2: Define Route Group
Use Route::group([...]) syntax for grouping.

Step 3: Add Middleware to Group
Apply 'middleware' => 'auth' to secure routes.

Step 4: Nest Routes Inside Group
Add multiple Route::get() inside the group.

Step 5: Use Prefix (Optional)
Add 'prefix' => 'admin' to organize URLs.

Step 6: Apply Namespace (Laravel <=7)
Use 'namespace' => 'Admin' if needed.

Step 7: Test Auth Middleware
Check if unauthorized users are redirected.

Step 8: Add More Groups
Group APIs or dashboards with different middlewares.

Step 9: Create Middleware (Optional)
Use php artisan make:middleware for custom logic.

Step 10: Register Middleware
Add it to app/Http/Kernel.php.