Table of Contents
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 8 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, Click here to Install Composer ? 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 8 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 8 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
- How to Create Multi Language Website in Laravel 8
- How To Read XML File in Laravel 8 – Example
- How To Upload And Save XML Data in Laravel 8
- Laravel 8 Ajax Post Request Tutorial
- Laravel 8 Authentication using Jetstream with Inertia Js
- Laravel 8 Authentication using Jetstream with Livewire
- Laravel 8 Authentication with Breeze Tutorial
- Laravel 8 Clear Cache of Route, View & Config
- Laravel 8 Cron Job Task Scheduling Tutorial
- Laravel 8 DataTable Ajax Pagination with Search And Sort
- Laravel 8 Firebase Push Notification Tutorial
- Laravel 8 Form Validation Methods
- Laravel 8 Installation Guide – PHP Framework
- Laravel 8 Layouts And Views Complete Guide
- Laravel 8 Routing Tutorial Step by Step Guide
- Laravel 8 Send Mail using Gmail SMTP Server
- Laravel 8 Send Push Notification to Android Using Firebase
- Laravel 8 Send Push Notification to IOS Using Firebase
- Laravel 8 Stub Customization
Hi, I am Sanjay the founder of ONLINE WEB TUTOR. I welcome you all guys here to join us. Here you can find the web development blog articles. You can add more skills in web development courses here.
I am a Web Developer, Motivator, Author & Blogger. Total experience of 7+ years in web development. I also used to take online classes including tech seminars over web development courses. We also handle our premium clients and delivered up to 50+ projects.