diff --git a/src/Middleware/InitializeTenancy.php b/src/Middleware/InitializeTenancy.php index bc1cbb1c..424110ca 100644 --- a/src/Middleware/InitializeTenancy.php +++ b/src/Middleware/InitializeTenancy.php @@ -28,6 +28,8 @@ class InitializeTenancy */ public function handle($request, Closure $next) { + if (tenancy()->initialized) return $next($request); + if (! in_array($request->getHost(), config('tenancy.exempt_domains', []), true)) { try { tenancy()->init($request->getHost()); diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index f7c562a4..9687fed6 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -78,8 +78,6 @@ class TenancyServiceProvider extends ServiceProvider __DIR__ . '/../assets/migrations/' => database_path('migrations'), ], 'migrations'); - $this->loadRoutesFrom(__DIR__ . '/routes.php'); - $this->app->make(Kernel::class)->prependMiddleware(Middleware\InitializeTenancy::class); /* @@ -92,6 +90,8 @@ class TenancyServiceProvider extends ServiceProvider Middleware\PreventAccessFromTenantDomains::class, ]); + $this->loadRoutesFrom(__DIR__ . '/routes.php'); + $this->app->singleton('globalUrl', function ($app) { if ($app->bound(FilesystemTenancyBootstrapper::class)) { $instance = clone $app['url']; diff --git a/src/routes.php b/src/routes.php index 69f51e70..7f1a5310 100644 --- a/src/routes.php +++ b/src/routes.php @@ -2,6 +2,8 @@ declare(strict_types=1); -Route::get('/tenancy/assets/{path}', 'Stancl\Tenancy\Controllers\TenantAssetsController@asset') - ->where('path', '(.*)') - ->name('stancl.tenancy.asset'); +Route::middleware(['tenancy'])->group(function () { + Route::get('/tenancy/assets/{path}', 'Stancl\Tenancy\Controllers\TenantAssetsController@asset') + ->where('path', '(.*)') + ->name('stancl.tenancy.asset'); +});