How to Grouping Controller Middleware Prefix Route

Sovary October 17, 2022 300
2 minutes read

Today, tutorial we are going to learn how to grouping in route file.This will helpfully organized your route link and maintain code effectively. If you have a few route links setup, maybe you don't want to do that. Anyway, let's say if you are building a complex application, you will have a lot of URL links as well.

We will give you examples of grouping routes with controller different namespace as well as group middleware where we want to apply in batch URL. Sometime you will see the URL access via https://cambotutorial.com/article/test.html this mean /article would be the prefix to identified, so we can grouping prefix URL if we have many URL and starting the same prefix I would recommend you to grouping those route thus, more standard and avoid human error URL access.

Please go check the following steps below to manage grouping route in different ways.

Example #1 - Grouping Controller

Normally, you have declare controllers in each route but you can group them without repeating the Controller class.

Assign Individual:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

Route::get('/article/{id}', [ArticleController::class,'show']);
Route::post('/article', [ArticleController::class,'add']);
Route::put('/article', [ArticleController::class,'update']);

Grouping:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

Route::controller(ArticleController::class)->group(function() 
{
    Route::get('/article/{id}', 'show');
    Route::post('/article', 'add');
    Route::put('/article', 'update');
});

Example #2 - Grouping Middleware

Assign Individual:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

Route::post('/article', [ArticleController::class,'add'])->middleware(['auth','locale']);
Route::put('/article', [ArticleController::class,'update'])->middleware(['auth','locale']);

Grouping:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

Route::middleware(['auth','locale'])->group(function() 
{
    Route::post('/article', [ArticleController::class,'add']);
    ....
});

Example #3 - Grouping Prefix

Prefix is used when we want to organized common URL structure. We can specify the prefix to all routes within the group by using prefix method.

Grouping:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

Route::prefix('/root')->group(function () 
{
    // To access this route /root/article
    Route::post('/article', [ArticleController::class,'add']);
    ....
});

Example #4 - Grouping All in One

We also can apply grouping prefix controller and middleware together.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ArticleController;

Route::prefix('/root')->controller(ArticleController::class)->middleware(['auth'])->group(function () 
{
    // To access this route /root/article
    Route::post('/article', 'add');
    ....
});

Example #5 - Nested Group

We can nested grouping inside other grouping as below example:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\UserController;

Route::middleware('auth')->group(function()
{
 
    Route::middleware('is_admin')->group(function()
    {
        Route::get("/admin",[AdminController::class,"index"]) // administrator routes
    });
    Route::middleware('is_user')->group(function()
    {
        Route::get("/user",[UserController::class,"index"]) // user routes
    });
});

You might also like... 

Laravel  PHP  Laravel 9  Laravel Middleware 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him