app->singleton(DomainService::class, function ($app) { $domainService = new DomainService($app['config']['domains']); // Validiere Konfiguration in der Development-Umgebung if (config('app.debug')) { $configErrors = $domainService->validateConfiguration(); if (!empty($configErrors)) { \Log::warning('Domain configuration errors detected', ['errors' => $configErrors]); } } return $domainService; }); // 2. DomainContext als Singleton registrieren. // Die Logik hier wird nur einmal pro Anfrage ausgeführt, wenn der // Context das erste Mal benötigt wird (z.B. in der Middleware). $this->app->singleton(DomainContext::class, function ($app) { /** @var DomainService $domainService */ $domainService = $app->make(DomainService::class); $request = $app->make('request'); // Analysiere den Host der aktuellen Anfrage $domainInfo = $domainService->parseDomain($request->getHost()); \Log::debug('DomainServiceProvider: domainInfo', [ 'domainInfo' => $domainInfo, 'host' => $request->getHost() ]); $userShop = null; // Wenn es sich um eine User-Shop-Domain handelt, versuche das Shop-Objekt zu finden. if ($domainInfo['type'] === 'user-shop' && $domainInfo['subdomain']) { $userShop = $domainService->getUserShop($domainInfo['subdomain']); // Wenn der Shop ungültig ist, wird der Typ auf 'unknown' gesetzt. if (!$userShop) { $domainInfo['type'] = 'unknown'; } } // Wenn es sich um die Haupt-Shop-Domain handelt (z.B. mivita.shop), // lade den konfigurierten Fallback-Shop. if ($domainInfo['type'] === 'main-shop' && $domainInfo['default_user_shop']) { $userShop = $domainService->getUserShop($domainInfo['default_user_shop']); } return DomainContext::fromArray($domainInfo, $userShop); }); } /** * Führt Aktionen nach der Registrierung aller Provider aus. * * @return void */ public function boot(Kernel $kernel) { // Registriert die neue Middleware global für die 'web' Gruppe. // Sie wird vor anderen Middleware ausgeführt, um den Kontext frühzeitig zu setzen. $kernel = $this->app->make(\Illuminate\Contracts\Http\Kernel::class); $kernel->prependMiddlewareToGroup('web', DomainResolver::class); } }