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

ChainofChaos's avatar

Flash session doesn't work after redirect

I'm on Episode 48 of Laravel 8 from scratch I followed the tutorial from the beginning however flash session doesn't work for me.

return redirect('/')->with('success','Your account has been created.');

I installed the laravel debugbar. I found out that the flash session only appears once in POST /register and I don't have the flash session on the homepage('/'). And the solution that works for me is to move \Illuminate\Session\Middleware\StartSession::class from web $middlewareGroups to $middleware in app\Http\Kernel.php and it works. Not sure why it didn't work the first time. Obviously, I add all the routes in web.php somehow the flash session didn't get passed on. Now the drawback is that every request will have the class.

0 likes
8 replies
Nakov's avatar

I would guess that another redirect happens before the homepage is displayed and your redirect above, either in a middleware or from the function that should return /.

ChainofChaos's avatar

@Nakov That's what I thought but I don't find any multiple redirects. From the laravel debugbar after POST it redirects to the homepage.

2021-10-01 14:38:59	GET	/
2021-10-01 14:37:09	POST /register
2021-10-01 14:36:57	GET	/register
    public function store()
    {
        $attributes = request()->validate([
            'name' => 'required|min:5|max:20',
            'username' => 'required|min:5|max:20|unique:users,username',
            'email' => 'required|max:255|email|unique:users,email',
            'password' => 'required|min:7|max:18'
        ]);
        User::create($attributes);
        return redirect('/')->with('success','Your account has been created.');
    }

This is the function for POST it only redirect once to the homepage.

Nakov's avatar

@ChainofChaos and can you show the / route? And also how do you try to display the success message?

ChainofChaos's avatar

@Nakov web.php

<?php

use App\Http\Controllers\PostController;
use App\Http\Controllers\RegisterController;
use Illuminate\Support\Facades\Route;

Route::get('/', [PostController::class, 'index'])->name('home');
Route::get('post/{post:slug}', [PostController::class, 'show']);

Route::get('register', [RegisterController::class, 'index']);
Route::post('register', [RegisterController::class, 'store']);

In the controller

    public function index()
    {
        return view('posts.index', [
            'posts' => Post::latest()->filter(request(['search', 'category','user']))->paginate(6)->withQueryString()
        ]);
    }

This is how I want to display in the homepage

    @if (session()->has('success'))
        <div class="fixed bg-blue-500 text-white py-2 px-4 rounded-xl bottom-3 right-3 text-sm">
            <p>{{ session('success') }}</p>
        </div>
    @endif
Nakov's avatar

@ChainofChaos hm, but your routes are not auth protected. What happens if you add the auth middleware to it:

Route::get('/', [PostController::class, 'index'])->middleware('auth')->name('home');

?

ChainofChaos's avatar

@Nakov I don't have a route for login yet. I am still on the register part. The tutorial doesn't use middleware and it still works, but I'm confused about it because I watched the video multiple times and I don't think I missed any parts. Maybe for further explanation(Don't know if this will help), the flash session on POST /register is inside the "new" array and there's none on the homepage ('/'). But after I moved the StartSession::class, the flash session from POST /register "new" array became available in the GET / inside the "old" array, and yes, it only appears once.

Nakov's avatar

@ChainofChaos that's my point. Session is started with the auth middleware, so you need to have protected route in order for the session to have the data.

You can easily create a different endpoint with view which will be auth protected and redirect there from the register action, and see if the session will be filled :) without moving around the middleware.

1 like
ChainofChaos's avatar

@Nakov Not sure what i did but what i just did was add middleware('guest') to the route and removed it again and now it works as expected. Thanks for the help btw.

Route::get('/', [PostController::class, 'index'])->middleware('guest')->name('home');

Then I tried to register and it worked the session showed up. After that i removed the middleware

Route::get('/', [PostController::class, 'index'])->name('home');

And removed the commented line

        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

to this

        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

and the flash session still works.

How is that possible? I don't really know I'm confused too but it works.

Please or to participate in this conversation.