Files
star-erp/app/Http/Middleware/UniversalTenancy.php
sky121113 c7e1154af8
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 47s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped
fix: 支援 Port 8081 直接訪問租戶 (IP-based tenancy support)
2026-01-16 08:58:32 +08:00

39 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Symfony\Component\HttpFoundation\Response;
class UniversalTenancy
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
// 判斷是否為中央域名
$centralDomains = config('tenancy.central_domains', []);
// [Hack] Demo 環境特殊規則:
// 如果是 8081 端口,強制視為租戶請求 (忽略中央域名檢查)
// 這樣可以讓 192.168.0.103:8081 直接訪問租戶,無需設定 hosts
if ($request->getPort() == 8081) {
return app(InitializeTenancyByDomain::class)->handle($request, $next);
}
if (in_array($request->getHost(), $centralDomains)) {
// 如果是中央域名,不進行租戶初始化,直接繼續往下執行 (使用預設資料庫)
return $next($request);
}
// 如果不是中央域名,嘗試透過域名初始化租戶
// 若找不到租戶InitializeTenancyByDomain 會拋出異常
return app(InitializeTenancyByDomain::class)->handle($request, $next);
}
}