How to group routes by domain in laravel?

Member

by rollin , in category: PHP Frameworks , 9 months ago

How to group routes by domain in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 9 months ago

@rollin 

To group routes by domain in Laravel, you can use the domain() method available in the Route facade. Here's an example:

1
2
3
4
5
6
7
8
9
// routes/web.php

Route::group(['domain' => 'example.com'], function () {
    Route::get('/', 'HomeController@index');
});

Route::group(['domain' => 'sub.example.com'], function () {
    Route::get('/', 'SubdomainController@index');
});


In this example, we have grouped routes under different domains - example.com and sub.example.com. You can define routes specific to each domain within their respective Route::group() closures.


Make sure to configure your local development environment to recognize these different subdomains or use a service like ngrok to test them locally.