Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ahoi's avatar
Level 5

Prevent redirection in Laravel 11 (API-only project)

Laravel 11 came with some kind of new project structure. And now I am facing a problem, which was never a problem to solve for me before:

I don't want to redirect a user to /login if the user is not authenticated. In such a szenario, I am getting:

Route [login] not defined

which originates from

vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:118

What is the correct way to deal with it now?

0 likes
4 replies
martinbean's avatar

@ahoi This isn’t new behaviour. If you send requests with a Accept: application/json header then you will get JSON responses, rather than HTML/redirect responses.

Maxwell2022's avatar

I faced the same issue migrating from the http kernel to the new bootstrap/app.php.

You can disable redirects for every request types:

$middleware->redirectTo(guests: fn() => null, users: fn() => null);
smares's avatar

This doesn't work: Illuminate\Auth\Middleware\RedirectIfAuthenticated::handle(): Return value must be of type Symfony\Component\HttpFoundation\Response, Illuminate\Routing\Redirector returned

Please or to participate in this conversation.