shouldBeSkipped(tenancy()->getRoute($request))) { return $next($request); } $domain = $this->getDomain($request); $subdomain = null; if (DomainTenantResolver::isSubdomain($domain)) { $subdomain = $this->makeSubdomain($domain); if ($subdomain instanceof Exception) { $onFail = static::$onFail ?? function ($e) { throw $e; }; return $onFail($subdomain, $request, $next); } } try { $this->tenancy->initialize( $this->resolver->resolve($subdomain ?? $domain) ); } catch (TenantCouldNotBeIdentifiedException $e) { if ($subdomain) { try { $this->tenancy->initialize( $this->resolver->resolve($domain) ); } catch (TenantCouldNotBeIdentifiedException $e) { $onFail = static::$onFail ?? function ($e) { throw $e; }; return $onFail($e, $request, $next); } } else { $onFail = static::$onFail ?? function ($e) { throw $e; }; return $onFail($e, $request, $next); } } return $next($request); } }