How to Grouping Controller Middleware Prefix Route

Sovary October 17, 2022 464
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

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him