I am doing something very similar. Here is how I am doing it:
Routes:
//this is for the main default domain that I host my main site/CMS
Route::group(['domain' => 'maindomainforcms.com'], function(){
Route::get('/', function(){ return 'this is the index page'; });
});
//this catches the rest of the domains and all their pages:
Route::any('{all}', 'SitesPublicController@index')->where('all', '.*');
SitesPublicController.php
use App\Http\Middleware\VerifiedDomain;
class SitesPublicController extends Controller {
public function __construct(){
$this->middleware('domain.verify');
}
public function index(VerifiedDomain $verified_domain){
$Site = $verified_domain->getSite();
$Page = $Site->getPageByRequest();//This method gets the url segments then selects the proper page
if(!$Page){ throw new NotFoundHttpException; }
return $Page->content;
}
}
VerifiedDomain.php middleware file:
use Closure;
use App\Domain;//this is just a model where I associate domains with sites
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class VerifiedDomain {
protected $Domain;
public function handle($request, Closure $next){
$domain = $_SERVER['SERVER_NAME'];
$Domain = Domain::where('domain', $domain)->first();
if(!$Domain){ throw new NotFoundHttpException; }
$Site = $Domain->site;
if(!$Site){
throw new NotFoundHttpException;
}
$this->Domain = $Domain;
return $next($request);
}
public function getDomain(){
return $this->Domain;
}
public function getSite(){
return $this->Domain->site;
}
}
I also assign the VerifiedDomain Class as a singleton so I can use Dependency Injection on the Controllers. I hope this helps.
EDIT: I left this out: I added this to my http kernel file:
protected $routeMiddleware = [
//other stuff...
'domain.verify' => 'App\Http\Middleware\VerifiedDomain',
];