Marijan
Marijan

Reputation: 1865

Routing: How to apply routes to all Domains except one

I have an application listening to three types of subdomain:

  1. Main application: www.website.com
  2. Custom subdomains handled by the same logic as the main application: *.app.website.com
  3. Admin panel: backend.website.com

Setup

The custom subdomains are coming in from a wildcard subdomain and managed by a Laravel middleware, which checks the database for validity and routes to www.website.com if an unknown subdomain request is coming in.

Otherwise the app behaves a little different on *.app.website.com here and there, but is mainly the same as the main application.

The requests on backend.website.com should be namespaced.

This setup mostly is working just fine.

Problem

The routes of the main application are accessible via the backend.website.com subdomain. I want the admin routes to only take the routes inside its group into account.

I tried to seperate the route groups by domain with the wildcard, but this will pass the wildcard as the first parameter to all controller actions, which is undesirable. I don’t need the wildcard at all, since this will be checked and handled by the middleware.

What I’ve tried

Route::domain('backend.website.com')->group(function () {
    Route::get('/', function () {
        …
    });
});


$appRoutes = function() {
    // this should not be hit by backend.website.com
    Route::get('/', function () {
        …
    })->name('home');

    …
};

Route::domain('www.website.com')->group($appRoutes);

// would probably work but passes $account to all 
// controller actions as first parameter which is undesirable
Route::domain('{account}.app.website.com')->group($appRoutes);

I’m hoping to be clear enough and would appreciate your help. 🤞🏼

Upvotes: 1

Views: 1217

Answers (1)

Marijan
Marijan

Reputation: 1865

Hooray, I think I found the answer only minutes after asking. Please correct me, if that’s bullshit.

Routes

Route::domain(config('app.admin_url'))->group(function () {
    Route::get('/', function () {
        …
    });
});


$appRoutes = function() {
    Route::get('/', function () {
        …
    })->name('home');
    …
};

// the middleware will redirect all requests from subdomains it does not know
// to the main application anyway, so trying to request main application routes 
// via backend.website.com will be redirected – yes!

Route::middleware(['\App\Http\Middleware\CheckSiteDomain'])->group($appRoutes);

CheckSiteDomain Middleware

public function handle($request, Closure $next)
{
    $url = url('');

    $app_url_regex = preg_quote(config('app.url'), '/');

    if (!preg_match("/^$app_url_regex/i", $url)) {

        // if current subdomain does not equal main application domain
        // try to find a matching "site" in the database and 
        // redirect if none is found

        $site = Site::byUrl($url, true);

        if (!$site)
            return redirect(config('app.url'));

    }

    return $next($request);
}

Thanks anyway! Sorry, I always struggle a long time before asking – probably even longer next time. ;-)

Upvotes: 1

Related Questions