2025-12-30 15:03:19 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Illuminate\Foundation\Application;
|
|
|
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
|
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
2026-01-16 08:39:25 +08:00
|
|
|
use Illuminate\Http\Middleware\TrustProxies;
|
2026-02-05 11:45:08 +08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2026-01-13 17:00:58 +08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException;
|
|
|
|
|
use Inertia\Inertia;
|
2026-02-05 11:45:08 +08:00
|
|
|
use Illuminate\Http\Request;
|
2026-01-16 08:39:25 +08:00
|
|
|
|
2025-12-30 15:03:19 +08:00
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
|
|
|
->withRouting(
|
|
|
|
|
web: __DIR__.'/../routes/web.php',
|
|
|
|
|
commands: __DIR__.'/../routes/console.php',
|
|
|
|
|
health: '/up',
|
|
|
|
|
)
|
|
|
|
|
->withMiddleware(function (Middleware $middleware): void {
|
2026-01-29 10:04:32 +08:00
|
|
|
// 信任所有代理(用於反向代理環境)
|
|
|
|
|
$middleware->trustProxies(at: '*');
|
|
|
|
|
|
2026-01-16 11:56:44 +08:00
|
|
|
// Tenancy 必須最先執行,確保資料庫連線在 Session 讀取之前建立
|
|
|
|
|
$middleware->web(prepend: [
|
2026-01-15 13:39:04 +08:00
|
|
|
\App\Http\Middleware\UniversalTenancy::class,
|
2026-01-16 11:56:44 +08:00
|
|
|
]);
|
|
|
|
|
$middleware->web(append: [
|
2025-12-30 15:03:19 +08:00
|
|
|
\App\Http\Middleware\HandleInertiaRequests::class,
|
|
|
|
|
]);
|
2026-01-13 17:00:58 +08:00
|
|
|
|
|
|
|
|
// 註冊 Spatie Permission 中間件別名
|
|
|
|
|
$middleware->alias([
|
|
|
|
|
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
|
|
|
|
|
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
|
|
|
|
|
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
|
|
|
|
|
]);
|
2025-12-30 15:03:19 +08:00
|
|
|
})
|
|
|
|
|
->withExceptions(function (Exceptions $exceptions): void {
|
2026-01-13 17:00:58 +08:00
|
|
|
// 處理 Spatie Permission 的 UnauthorizedException
|
|
|
|
|
$exceptions->render(function (UnauthorizedException $e) {
|
2026-02-05 11:45:08 +08:00
|
|
|
return Inertia::render('Error/Index', ['status' => 403])
|
|
|
|
|
->toResponse(request())
|
|
|
|
|
->setStatusCode(403);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 處理 404 NotFoundHttpException
|
|
|
|
|
$exceptions->render(function (NotFoundHttpException $e) {
|
|
|
|
|
return Inertia::render('Error/Index', ['status' => 404])
|
|
|
|
|
->toResponse(request())
|
|
|
|
|
->setStatusCode(404);
|
2026-01-13 17:00:58 +08:00
|
|
|
});
|
|
|
|
|
|
2026-02-05 11:45:08 +08:00
|
|
|
// 處理其他一般的 HttpException (包含 403, 419, 429, 500, 503 等)
|
2026-01-13 17:00:58 +08:00
|
|
|
$exceptions->render(function (HttpException $e) {
|
2026-02-05 11:45:08 +08:00
|
|
|
$status = $e->getStatusCode();
|
|
|
|
|
return Inertia::render('Error/Index', ['status' => $status])
|
|
|
|
|
->toResponse(request())
|
|
|
|
|
->setStatusCode($status);
|
2026-01-13 17:00:58 +08:00
|
|
|
});
|
2025-12-30 15:03:19 +08:00
|
|
|
})->create();
|
2026-01-13 17:00:58 +08:00
|
|
|
|