Concept of Route Group in Laravel 8 Tutorial

Share this Article
Reading Time: 4 minutes
1,935 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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.

Find More on Laravel 8 Articles here