I want to prevent access to some of my app routes from other domain except listed. It success using below code:
$loginRoutes = function() { Route::get('/', 'HomeController@index')->name('home'); }; Route::domain('domain1.com')->group($loginRoutes); Route::domain('domain2.com')->group($loginRoutes); Route::domain('localhost')->group($loginRoutes); But the problem is when I call {{route('home')}}, the URL always becomes the domain at the last line of the routes.php(at above case is http://localhost ). How to make it to current domain?
My current solution:
if (isset($_SERVER["HTTP_HOST"]) && $_SERVER["HTTP_HOST"] == "domain1.com") { Route::domain('domain1.com')->group($loginRoutes); }elseif (isset($_SERVER["HTTP_HOST"]) && $_SERVER["HTTP_HOST"] == "domain2.com") { Route::domain('domain2.com')->group($loginRoutes); } It's work but I think it's dirty. I have a lot of domains/subdomain and also the routes too.
I need solution on route directly, because I have a lot of routes, if I update each controller it's will take a long time. Maybe edit route provider or laravel vendor code is also no problem.
route()will return with your application'sAPP_URL. I believe you've set it to localhost right now, so it would return localhost in that case. Also what are you using for your web server? I was thinking, wouldn't it be better to define virtual hosts?