Search code examples
phplaravelmamplaravel-5.7mamp-pro

Multiple Domains w/ Single Laravel Install


Sort of consolidating a few questions that are old, unresolved, and kind of in keeping with my own problem.

Routes file:

echo url()->current() ."<br>";

echo request()->getHost();

Route::domain('pro.local')->group(function () {
  Route::get('/', function () {
    dd('HELLO');
  });
});

Route::group(['domain' => 'pro.local'], function() {
    dd('PRO');
});

Route::group(['domain' => 'media.local'], function() {
    dd('MEDIA');
});

Route::group(['domain' => 'software.local'], function() {
    dd('SOFTWARE');
});

Route::get('/', function () {
    return view('welcome');
});

Desire & environment: Three domains pro.local, media.local, and software.local all pointing to the same public folder using MAMP PRO 5.2 and Laravel 5.7. This is all I have done to the project so far.

Hypothesis: Using Route::domain or Route::group should result in returning the dd() text or the welcome template.

So far: I know the mono-repo setup I'm using works because I've had the three sites running off the mono-repo for about 3 years and can share the services and what not across projects. With that said, it's annoying to have to SSH into three separate folders to run composer update and npm update; especially when the composer.json and package.json files for each project is essentially the same...I currently use gulp to move and copy files around to keep things in sync.

Problem: No matter the domain, only PRO gets echoed.

enter image description here enter image description here enter image description here

It seems to skip the Route::domain and settle on the first Route::group, as demonstrated by moving the dd('MEDIA') call to the top.

enter image description here


Solution

  • Code inside a Route::group always gets run, as Laravel compiles the various route definitions for later use. Thus, your dd() is getting executed as Laravel builds the list of routes, short circuiting the entire process regardless of what domain you're on.

    If you put each of your debugging dd calls within a Route::get('/', function () {}) inside each route group (like you do the first time with the Route::domain('pro.local') bit), you'd get the results you expected.

    Route::group(['domain' => 'pro.local'], function() {
        Route::get('/', function () {
            dd('PRO');
        });
    });
    
    Route::group(['domain' => 'media.local'], function() {
        Route::get('/', function () {
            dd('MEDIA');
        });
    });
    
    Route::group(['domain' => 'software.local'], function() {
        Route::get('/', function () {
            dd('SOFTWARE');
        });
    });
    
    Route::get('/', function () {
        return view('welcome');
    });
    

    ALTERNATIVE: Switching them all to use Route::domain also ended up working per discovery on another forum.