Why don't you just use the auth middleware on the route?
Route::get('/blogs/protected/{title}', ProtectedPostsController::class)
->middleware('auth')->name('blogs.protected.show');
Or a custom middleware if needed.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm working on this blog type of website which has some posts locked behind auth. When trying to access a post that is locked behind auth, in the controller I check if they're logged in, and if not set in the session the URL of the post, and then redirect to login page using the following code. On the login controller I can then get that post URL from the session and redirect to it fine.
session(['login-redirect' => url()->full()]);
redirect()->route('login')->send();
However when doing this process from a fresh session (i.e. open a private window and go straight to the post URL), 'login-redirect' doesn't get retained in the session after redirecting to the login page. It's like the session is reset somewhere in-between. If I go back to the post URL and try again it works, just not on the very first load of the session.
Any ideas why this would be happening?
Please or to participate in this conversation.