mivita/dev/app-bak/Providers/RouteServiceProvider.php
2025-10-20 17:42:08 +02:00

130 lines
4 KiB
PHP

<?php
namespace App\Providers;
use App\Domain\DomainContext;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* @var string
*/
public const HOME = '/';
/**
* The controller namespace for the application.
*
* @var string|null
*/
protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
// $this->configureRateLimiting();
$this->routes(function () {
// API-Routen werden global geladen
Route::domain('api.' . config('app.domain') . config('app.tld_care'))
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
// Web-Routen werden domain-bewusst geladen
Route::middleware('web')
->namespace($this->namespace)
->group(function () {
$this->loadDomainAwareRoutes();
});
});
}
/**
* Lädt Routen basierend auf dem aktuellen Domain-Kontext.
*/
protected function loadDomainAwareRoutes(): void
{
/** @var DomainContext $context */
$context = app(DomainContext::class);
$this->loadSharedRoutes();
if (config('app.debug')) {
\Log::channel('domain')->info('loadDomainAwareRoutes', ['context' => $context]);
}
match ($context->type) {
'main' => $this->loadDomainRoutes('main', 'main.php'),
'main-shop' => [
$this->loadDomainRoutes('shop', 'shop.php'),
$this->loadDomainRoutes('portal', 'portal.php'),
],
'user-shop' => [
$this->loadDomainRoutes('user-shop', 'user-shop.php'),
$this->loadDomainRoutes('portal', 'portal.php'),
],
'crm' => $this->loadDomainRoutes('crm', 'crm.php'),
'portal' => $this->loadDomainRoutes('portal', 'portal.php'),
'checkout' => $this->loadDomainRoutes('checkout', 'checkout.php'),
default => $this->loadAllDomainRoutesForCaching(),
};
}
/**
* Lädt eine spezifische Routendatei für eine Domain.
*/
protected function loadDomainRoutes(string $domainType, string $fileName): void
{
$domain = config("domains.domains.{$domainType}.host");
Route::domain($domain)
->group(base_path('routes/domains/' . $fileName));
}
/**
* Lädt alle domainspezifischen Routen.
* Wird als Fallback für das Route-Caching benötigt.
*/
protected function loadAllDomainRoutesForCaching(): void
{
if (app()->routesAreCached()) {
return;
}
$this->loadDomainRoutes('main', 'main.php');
$this->loadDomainRoutes('shop', 'shop.php');
$this->loadDomainRoutes('crm', 'crm.php');
$this->loadDomainRoutes('portal', 'portal.php');
$this->loadDomainRoutes('checkout', 'checkout.php');
$this->loadDomainRoutes('user-shop', 'user-shop.php');
}
/**
* Lädt Routen, die auf allen Domains verfügbar sein sollen.
*/
protected function loadSharedRoutes(): void
{
// Lädt Routen, die auf allen Domains verfügbar sein sollen.
Route::group([], base_path('routes/shared/common.php'));
}
/**
* Konfiguriert die Rate-Limiter für die Anwendung.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}