Concept of Route Group in Laravel 8 Tutorial

Reading Time: 4 minutes
5,848 Views

If we are building large project in web, then surely we have several routes. Also we have several modules inside that application. Inside this article, we will see that How we manage routes as route group in laravel 8.

This article is very interesting to learn. This will be step by step guide for the concept of Route group tutorial.

Routes group may contains middleware protected routes, prefix etc. We will see all these concept here in detailed way.

In simple terms, Route group is a collection of routes. Route groups allow to share route attributes, such as middleware, prefix, across a large number of routes without needing to define those attributes on each individual route.

To learn custom route files in laravel 8, Click here.

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Syntax – Route Group

There is a very basic syntax in laravel to define Route group in laravel 8.

Route::group( [ ] , callback);

Inside first array we can pass prefix, middleware etc.


Route Group with Route Prefix

Let’s say we have an application in which we have modules as Admin, Customer.

Example

Routes of Admin module as

# Route 1
/admin/create-user

# Route 2
/admin/list-users

# Route 3
/admin/edit-user

Routes of Customer module as

# Route 1
/customer/list-purchase

# Route 2
/customer/list-blogs

# Route 3
/customer/create-blog

Application Routes without group concept

...

// Admin Routes
Route::get("admin/create-user", [AdminController::class, "createUser"]);
Route::get("admin/list-users", [AdminController::class, "listUsers"]);
Route::get("admin/edit-user", [AdminController::class, "editUser"]);

// Customer Routes
Route::get("customer/list-purchase", [CustomerController::class, "listPurchase"]);
Route::get("customer/list-blogs", [CustomerController::class, "listBlogs"]);
Route::get("customer/create-blog", [CustomerController::class, "createBlog"]);

...

Each time in route, we are defining admin/ and customer/ with route. This is not a good practise.

Application Routes with route group concept

...

// Admin Routes
Route::prefix("admin")->group(function(){
   Route::get("create-user", [AdminController::class, "createUser"]);
   Route::get("list-users", [AdminController::class, "listUsers"]);
   Route::get("edit-user", [AdminController::class, "editUser"]);
});

// Customer Routes
Route::prefix("customer")->group(function(){
   Route::get("list-purchase", [CustomerController::class, "listPurchase"]); 
   Route::get("list-blogs", [CustomerController::class, "listBlogs"]);
   Route::get("create-blog", [CustomerController::class, "createBlog"]);
});

...

Route Group with Middleware

Middleware is the level of security. Applying middleware into group of routes means we are protecting route groups.

# Middleware with single route

Route::get("sample-test", [AdminController::class, "createUser"])->middleware("first");

# Middleware with route group

Route::middleware(['first', 'second'])->group(function () {

   Route::get("create-user", [AdminController::class, "createUser"]);
   Route::get("list-users", [AdminController::class, "listUsers"]);
   Route::get("edit-user", [AdminController::class, "editUser"]);

});

Here, applying first and second middleware into group of routes.

We hope this article helped you to learn about Concept of Route Group in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more