One way to handle multiple domains in Laravel is to use the Route::group method to define routes for each domain. Here's an example:
Route::group(['domain' => 'example.com'], function () {
// Define routes for example.com
});
Route::group(['domain' => 'example.net'], function () {
// Define routes for example.net
});
Route::group(['domain' => 'example.org'], function () {
// Define routes for example.org
});
You can also use middleware to handle domain-specific logic. For example, you could create a middleware that sets a variable based on the current domain, and then use that variable in your controllers or views. Here's an example:
namespace App\Http\Middleware;
use Closure;
class SetDomainVariable
{
public function handle($request, Closure $next)
{
$domain = $request->getHost();
if ($domain === 'example.com') {
$request->attributes->set('domain', 'example.com');
} elseif ($domain === 'example.net') {
$request->attributes->set('domain', 'example.net');
} elseif ($domain === 'example.org') {
$request->attributes->set('domain', 'example.org');
}
return $next($request);
}
}
Then, you can apply this middleware to your routes or controllers:
Route::group(['middleware' => 'setDomainVariable'], function () {
Route::get('/', function () {
$domain = request()->attributes->get('domain');
return view('welcome', compact('domain'));
});
});
This will set the $domain variable based on the current domain, which you can then use in your views.