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-01-13 17:00:58 +08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException;
|
|
|
|
|
use Inertia\Inertia;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
2026-01-16 08:39:25 +08:00
|
|
|
// 信任所有代理(用於反向代理環境)
|
|
|
|
|
TrustProxies::at('*');
|
|
|
|
|
|
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 {
|
|
|
|
|
$middleware->web(append: [
|
2026-01-15 13:39:04 +08:00
|
|
|
\App\Http\Middleware\UniversalTenancy::class,
|
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) {
|
|
|
|
|
return Inertia::render('Error/403')->toResponse(request())->setStatusCode(403);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 處理一般的 403 HttpException
|
|
|
|
|
$exceptions->render(function (HttpException $e) {
|
|
|
|
|
if ($e->getStatusCode() === 403) {
|
|
|
|
|
return Inertia::render('Error/403')->toResponse(request())->setStatusCode(403);
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-12-30 15:03:19 +08:00
|
|
|
})->create();
|
2026-01-13 17:00:58 +08:00
|
|
|
|