Jon Kim
Jon Kim

Reputation: 117

Why aren't Laravel's flash messages persisting after redirect in Laravel 11, and how can I fix this while still using the redirect()->with() method?

Laravel 11: Can't Get Both Normal Sessions and Flash Messages Working Simultaneously

Problem

I'm experiencing a conflict in Laravel 11 where I can only get either normal sessions OR flash messages to work, but not both:

  1. When middleware is commented out: Flash messages work, but normal sessions don't persist
  2. When middleware is uncommented: Normal sessions work, but flash messages disappear after redirect

Code

Controller:

public function switchLang(Request $request, $locale)
{
    $request->session()->put('locale', $locale); // Normal session
    
    return redirect()
        ->back()
        ->with('flash_message', '语言更换成功.') // Flash message
        ->with('flash_type', 'success');
}

View:

{{-- Flash Message --}}
@if(session('flash_message'))
    <div class="alert alert-{{ session('flash_type', 'info') }}">
        {{ session('flash_message') }}
    </div>
@endif

{{-- Normal Session --}}
{{ session('locale') }}

app.php Configuration that makes flash work but breaks normal sessions:

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        // Commenting these out makes flash messages work but breaks normal sessions
        // $middleware->use([
        //     \Illuminate\Session\Middleware\StartSession::class,
        //     \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //     \App\Http\Middleware\VerifyCsrfToken::class,
        // ]);
    })
    ->create();

app.php Configuration that makes normal sessions work but breaks flash:

return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware) {
        // Uncommenting these makes normal sessions work but breaks flash messages
        $middleware->use([
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ]);
    })
    ->create();

config/session.php:

return [
    'driver' => env('SESSION_DRIVER', 'database'),
    'lifetime' => env('SESSION_LIFETIME', 120),
    'expire_on_close' => false,
    'encrypt' => env('SESSION_ENCRYPT', false),
    'files' => storage_path('framework/sessions'),
    'connection' => env('SESSION_CONNECTION'),
    'table' => env('SESSION_TABLE', 'sessions'),
    'store' => env('SESSION_STORE'),
    'lottery' => [2, 100],
    'cookie' => env(
        'SESSION_COOKIE',
        Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
    ),
    'path' => env('SESSION_PATH', '/'),
    'domain' => env('SESSION_DOMAIN'),
    'secure' => env('SESSION_SECURE_COOKIE'),
    'http_only' => env('SESSION_HTTP_ONLY', true),
    'same_site' => env('SESSION_SAME_SITE', 'lax'),
    'partitioned' => env('SESSION_PARTITIONED_COOKIE', false),
];

Debug Logs

When flash works (but normal sessions don't):

[2024-11-21 02:26:00] First Request:
{
    "_token": "o5IooR4tva9abbacvE14SPzS1EKR7XKj7RKO8UWt",
    "_flash": {"old": ["flash_message", "flash_type"], "new": []},
    "flash_message": "语言更换成功.",
    "flash_type": "success"
}

When normal sessions work (but flash doesn't):

{
    "_token": "o5IooR4tva9abbacvE14SPzS1EKR7XKj7RKO8UWt",
    "_flash": {"old": [], "new": []},
    "locale": "en"
}

Environment

What I've Tried

  1. Using session()->flash()
  2. Using redirect()->with()
  3. Toggling middleware configuration in app.php
  4. Checking middleware order
  5. Verified session table exists and migrations are up to date
  6. Created custom flash implementation (works, but want to use Laravel's built-in functionality)

Tags: laravel, php, sessions, flash-messages, laravel-11

Upvotes: 2

Views: 123

Answers (0)

Related Questions