How To Work with Laravel 10 Route Groups Tutorial

Reading Time: 4 minutes
361 Views

Route groups in Laravel 10 give a robust and organised approach to collect and manage routes with shared attributes like middleware, prefixes, and namespaces.

Route groups allow you to simplify your application’s routing by applying common settings to numerous routes, resulting in simpler, more maintainable, and easier-to-manage code.

This tutorial will walk you through the process of working with route groups in Laravel 10. We’ll look at how to use this functionality to construct route groups, specify shared properties, and organise routes efficiently.

Read More: Laravel 10 Redirect Route with Query String Parameters

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Laravel Route Group (Syntax)

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.

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

Route::group( [ ] , callback);

Inside first array we can pass prefix, middleware etc.

Examples: Route Group with Route Prefix

Suppose, you have an application in which you have modules as Admin, Customer.

Example Routes

Routes of Admin module as

# Route 1
/admin/create-user

# Route 2
/admin/list-users

# Route 3
/admin/edit-user

Read More: Laravel 10 Pagination with Bootstrap Example Tutorial

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

Here, you can see all routes are defined with route prefixed.

//...
// 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

Adding concept of prefix() method to create route groups.

// 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

Laravel Middleware is a component of the Laravel web application framework, a popular PHP framework for developing web applications. In the application’s HTTP pipeline, middleware functions as a bridge between a request and its matching response.

It allows you to filter and change incoming HTTP requests before they reach their intended destination (often a controller action) and before the answer is generated.

// 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.

Read More: Laravel 10 HTTP cURL POST Request with Headers Tutorial

We hope this article helped you to learn about How To Work with Laravel 10 Route Groups Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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