Added API
This commit is contained in:
+30
-4
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Middleware\HandleInertiaRequests;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Foundation\Configuration\Exceptions;
|
||||
use Illuminate\Foundation\Configuration\Middleware;
|
||||
@@ -11,6 +12,7 @@ use Illuminate\Http\Response;
|
||||
use Spatie\Permission\Middleware\PermissionMiddleware;
|
||||
use Spatie\Permission\Middleware\RoleMiddleware;
|
||||
use Spatie\Permission\Middleware\RoleOrPermissionMiddleware;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
|
||||
return Application::configure(basePath: dirname(__DIR__))
|
||||
@@ -19,6 +21,7 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
api: __DIR__.'/../routes/api.php',
|
||||
commands: __DIR__.'/../routes/console.php',
|
||||
health: '/up',
|
||||
apiPrefix: '',
|
||||
)
|
||||
->withMiddleware(function (Middleware $middleware): void {
|
||||
$middleware->web(append: [
|
||||
@@ -33,9 +36,26 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
//
|
||||
})
|
||||
->withExceptions(function (Exceptions $exceptions): void {
|
||||
$exceptions->render(function (NotFoundHttpException $e, Request $request) {
|
||||
if ($request->getHost() !== config('app.api_domain')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($e->getPrevious() instanceof ModelNotFoundException) {
|
||||
return response()->json(['message' => 'Resource not found.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Not found.'], 404);
|
||||
});
|
||||
|
||||
$exceptions->respond(function ($response, Throwable $e, Request $request) {
|
||||
$status = $response->getStatusCode();
|
||||
|
||||
// API domain: never touch the response, let Laravel's own JSON rendering stand.
|
||||
if ($request->getHost() === config('app.api_domain')) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$errors = [
|
||||
403 => [
|
||||
'title' => "The Cockpit is Off Limits",
|
||||
@@ -64,10 +84,8 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
];
|
||||
|
||||
$isLocal = app()->environment(['local', 'testing']);
|
||||
$handled = array_keys($errors);
|
||||
$friendlyErrorsOnLocal = [404, 403];
|
||||
|
||||
// In local/testing, only handle 404. In production, handle all.
|
||||
$shouldHandle = isset($errors[$status]) && (
|
||||
!$isLocal || in_array($status, $friendlyErrorsOnLocal)
|
||||
);
|
||||
@@ -88,5 +106,13 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
])
|
||||
->toResponse($request)
|
||||
->setStatusCode($status);
|
||||
});
|
||||
})->create();
|
||||
})
|
||||
->shouldRenderJsonWhen(function ($request, Throwable $e) {
|
||||
if ($request->getHost() === config('app.api_domain')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $request->expectsJson();
|
||||
});
|
||||
})
|
||||
->create();
|
||||
|
||||
Reference in New Issue
Block a user