How to group routes based on sub-domain laravel?

  • 04-11-2022
  • 582
  • Laravel 9
  • Haresh Chauhan

If you want to create route grouping based on your sub-domain, This post will help you to make your routing how to make grouping based on a sub-domain. This is very to understand this and setup in your route file.

If you wish to make your routing group based on a sub-domain for the make panel and front site separate. like the main domain for the front website and the admin panel for the sub-domain, so you do it very simply by referring to this post.

How to group routes based on sub-domain?

Route::domain('{sub_domain}.example.com')->group(function () {
     Route::get('user/{id}', function ($sub_domain, $id) {
          // YOUR CONTROLLER LOGIC GOES HERE
     });
});

Prefix Url

You can call a method prefix, using the prefix route method you can apply any prefix URL path to the app that grouping routes.

Route::prefix('admin')->group(function () {
     Route::get('user',[HomeController::class,index]);
});

Prefix Route

In case, if you want to give a prefix route name for the specific route groups, you can call the name route method in the route grouping.

Route::name('admin.')->group(function () {
     Route::get('user',[HomeController::class,index])->name('dashboard');
});

Call Middleware

If you want to call specific middleware to the route grouping using the middleware method you can use it, This method also accepts the array, so you can check more then one middleware.

Route::middleware(['auth.check'])->group(function () {
     Route::get('user',[HomeController::class,index])->name('dashboard');
});

Create Custom Route File With New Service Provider Laravel 8 9

Laravel Routing Multiple Method Use In Single Route

Route Resource Controller Laravel


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :