简体   繁体   中英

Session data not preserved after redirection

I'm trying to implement some custom flash messages and I'm having some issues with the session data being destroyed after a redirect.

Here's how I create my flash messages :

flash('Your topic has been created.');

Here's the declaration of the flash() function :

function flash($message, $title = 'Info', $type = 'info')
{   
    session()->flash('flash', [
        'message' => $message,
        'title' => $title,
        'type' => $type,        
    ]); 
}

And here is how I'm checking the session/displaying the flash messages, using SweetAlerts. This code is included at the bottom of the main layout file that I'm extending in all my Blade templates.

@if(Session::has('flash'))
    <script>
        $(function(){
            swal({
                title: '{{ Session::get("flash.title") }}',
                text : '{{ Session::get("flash.message") }}',
                type : '{{ Session::get("flash.type") }}',
                timer: 1500,
                showConfirmButton: false,           
            })
        });         
    </script>
@endif

The code above will work if I call the flash() function before displaying a view, like so :

public function show($slug)
{
    flash('It works!');
    return view('welcome');
}

However, it will not work if I call it before doing a redirect to another page, like so :

public function show($slug)
{
    flash('It does not work');
    return redirect('/');
}

Why is the session data lost on redirect? How can I make it persists so that I can display my flash message?

I found out that it is necessary to apply the web middleware on all routes. Drown has mentioned to do so, but since March 23st 2016, Taylor Otwell changed the default RouteServiceProvider at https://github.com/laravel/laravel/commit/5c30c98db96459b4cc878d085490e4677b0b67ed

By that change the web middleware is applied automatically to all routes. If you now apply it again in your routes.php, you will see that web appears twice on the route list ( php artisan route:list ). This exactly makes the flash data discard.

Also see: https://laracasts.com/discuss/channels/laravel/session-flash-message-not-working-after-redirect-route/replies/159117

It turns out that with Laravel 5.2, the routes have to be wrapped in the web middleware for the session to work properly.

This fixed it :

Route::group(['middleware' => ['web']], function () {
    // ...
    Route::post('/topics/{slug}/answer', 'PostsController@answer');
    Route::post('/topics/{slug}/unanswer', 'PostsController@unanswer');
    Route::post('/topics/{slug}/delete', 'PostsController@delete');
});

The issue i had was Session::save() preventing swal from showing after redirect.

so you need to remove Session::save() or session()->save(); from middleware

Please check APP/kernel.php

\\Illuminate\\Session\\Middleware\\StartSession::class,

is define multiple times

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class,
  ];

protected $middlewareGroups = [
    '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,
    ],

You can comment any one or delete it. We need to define one time only.

With Laravel 5.2.34, all routes are using web middleware by default.

Therefore, change this:

Route::group(['middleware' => ['web']], function () { // This will use 2 web middleware

    // ...

    Route::post('/foo', 'FooController@foo');

});

To this:

Route::group([], function () { // This will use the default web middleware

    // ...

    Route::post('/foo', 'FooController@foo');

});

And then in your controller you could use:

class FooController extends Controller
{
    ...

    public foo() 
    {
        ...

        return redirect('/foo')->withSuccess('Success!!');
        // or
        return redirect('/foo')->with(['success' => 'Success!!']);
    }

    ...
}

Redirect with flash data is done like this:

redirect("/blog")->with(["message"=>"Success!"]);

In early Laravel 5.2 versions, all of your Flash and Session data are stored only if your routes are inside web middleware group.

As of Laravel 5.2.34 , all routes are using web middleware by default . If you will put them into middleware web group again, you will apply web middleware on your routes twice - such routes will be unable to preserve Flash or Session data.

Check your App\\Kernel.php file. There may be multiple lines of \\Illuminate\\Session\\Middleware\\StartSession::class, Comment one from $middlewareGroups.

protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

protected $middlewareGroups = [
        '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,
        ],

Additional to @Harry Bosh answer,

In Laravel there an issue when Session::save() happen inside the middleware, this make _flash session gone after redirection happen

this can be fix by using alternative :

// replace your Session::save() to this

session(['yoursessionvar' => $examplevar]); // this will save laravel session

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM