did you mean to write '/routes/api.php'
Feb 26, 2024
8
Level 1
API on laravel 11
So I decided to use Laravel 11 for my new application (why not). And I'm a little confused with api requests.
I configurate my app (I have only api cases):
return Application::configure(basePath: dirname(__DIR__))
->withProviders([
...(require __DIR__ . '/providers.php'),
],
false
)
->withCommands([ __DIR__ . '/Command'])
->withRouting(
api: __DIR__ . '/routes.api.php',
apiPrefix: ''
)
->withExceptions(function (Exceptions $exceptions): void {
$exceptions->renderable(function (\Throwable $throwable): JsonResponse {
return handleApiException($throwable);
});
})
->create();
but, but I feel that my application does not understand that it is an api.
I recieve this error: Route [login] not defined.
Do you remember there used to be a middleware Authenticate that was available in the skeleton? I used to do this:
class Authenticate extends Middleware
{
protected function redirectTo(Request $request): ?string
{
if (str_starts_with($request->getUri(), config('app.api_url'))) {
return null;
}
if ($request->expectsJson() === false) {
return route('login');
}
return null;
}
}
but now this middleware is hidden in the core.
So if my requests do not use special json headers, then the application does not understand that this is an api.
I can make some kind of workaround:
$request->headers->add([
'Accept' => '.../json',
]);
this will solve the problem, but something tells me that it wasn’t intended that way.
Please or to participate in this conversation.