Skip to content

Instantly share code, notes, and snippets.

@tarikmanoar
Created July 12, 2023 03:44
Show Gist options
  • Save tarikmanoar/34138d4c34f6c52f2dd58b7a24e281dc to your computer and use it in GitHub Desktop.
Save tarikmanoar/34138d4c34f6c52f2dd58b7a24e281dc to your computer and use it in GitHub Desktop.
This script used for creating subdomain and / path base url

In ENV

WEBSITE_HOST="example.com"

Web.php

$websiteHost = env('WEBSITE_HOST');
$currentHost = request()->getHost();
if (!app()->runningInConsole()) {
    if (substr($_SERVER['HTTP_HOST'], 0, 4) === 'www.') {
        $currentHost = 'www.' . env('WEBSITE_HOST');
    }
}
$domain = ($currentHost === $websiteHost) ? $websiteHost : '{subdomain}.' . $websiteHost;
$prefix = ($currentHost === $websiteHost) ? '/{username}' : '';
// dd($websiteHost,$currentHost,$domain, $prefix);
Route::domain($domain)->prefix($prefix)->group(function () {
    Route::get('/', [SubdomainController::class, 'index'])->name('index');
});

Route::domain($websiteHost)->group(function () {
    Route::get('/', [MainController::class, 'index'])->name('index');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment