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 9.
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.
Learn More –
- Laravel 9 Redirect Route with Query String Parameters
- Laravel 9 How To Add Image Into PDF Using DomPDF Library
- Laravel 9 How To Add Pagination To Table Tutorial
- Laravel 9 How To Change Address & Name in Email
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.
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.
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 9 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.