app/bootstrap/app.php

72 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2025-10-13 14:57:11 +02:00
<?php
use App\Http\Middleware\ForceJsonResponse;
use App\Http\Middleware\HandleResourceNotFound;
use App\Services\OutputService;
use DishPlanner\Dish\Controllers\DishController;
use DishPlanner\Schedule\Console\Commands\GenerateScheduleCommand;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
2025-12-27 21:18:09 +01:00
// Apply ForceJsonResponse only to API routes
$middleware->api(ForceJsonResponse::class);
2025-10-13 14:57:11 +02:00
})
->withExceptions(function (Exceptions $exceptions) {
$exceptions->shouldRenderJsonWhen(function (Request $request, Throwable $e) {
if ($request->is('api/*')) {
return true;
}
return $request->expectsJson();
});
/** @var OutputService $outputService */
$outputService = resolve(OutputService::class);
$exceptions->render(function (ValidationException $e, Request $request) use ($outputService) {
if ($request->is('api/*') || $request->expectsJson()) {
return response()->json(
$outputService->response(false, null, [$e->getMessage()]),
404
);
}
});
2025-10-13 14:57:11 +02:00
$exceptions->render(function (NotFoundHttpException $e, Request $request) use ($outputService) {
if ($request->is('api/*') || $request->expectsJson()) {
return response()->json(
$outputService->response(false, null, ['MODEL_NOT_FOUND']),
404
);
}
});
2025-10-13 14:57:11 +02:00
$exceptions->render(function (AccessDeniedHttpException $e, Request $request) use ($outputService) {
if ($request->is('api/*') || $request->expectsJson()) {
return response()->json(
$outputService->response(false, null, [$e->getMessage()]),
403
);
}
});
2025-10-13 14:57:11 +02:00
})
->withCommands([
GenerateScheduleCommand::class,
])
->create();