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

lauriek's avatar

Laravel 5.0 to 5.5 upgrade, session flash messages not working

Another question relating to my project which is being updated from Laravel 5.0 to 5.5

I have code which passes error messages between pages like so:-

    Session::flash('flash_warning', "Are you sure you are Human?");
    return Redirect::back()->withInput();

And in the base controller (constructor) code (which all my controllers extend)

    if (Session::has('flash_warning')) {
        View::share('flash_warning', Session::get('flash_warning'));
    }

But this is not working, the session appears not to have the flash_warning value set although I can see it's being flashed before the redirect.

Is this the same issue as I had with the Auth::user() where the middleware hasn't run by the time the constructors run? And if so how would I get around that? I really don't want to have to implement that code in like every method which needs it. That's the whole point of having it in a method called by the base controller constructor...

0 likes
9 replies
mstrauss's avatar

@lauriek

The docs from 5.0 state the below (which is what you have)

Sometimes you may wish to store items in the session only for the next request. You may do so using the Session::flash method:

Session::flash('key', 'value');

But the 5.5 docs display it slightly differently, see below:

Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method. Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted. Flash data is primarily useful for short-lived status messages:

$request->session()->flash('status', 'Task was successful!');

While they appear to be equivalent, just using a helper versus a Facade, try replacing your flash message creation in the controller with the above ^^^

mstrauss's avatar

I may have been barking up the wrong tree in my prior answer. I'm actually thinking it's an issue with how you are sharing the view, i.e. below in your Base controller:

if (Session::has('flash_warning')) {
        View::share('flash_warning', Session::get('flash_warning'));
    }

Try this instead (from the docs):

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
lauriek's avatar

Many thanks, that's exacy what I'm doing. Initially tried to upgrade but the amount of errors was huge, so I started a blank 5.5 project and am gradually pulling in my old code and fixing as I go.

lauriek's avatar

Many thanks, I'll try this as soon as I'm back on the computer..

lauriek's avatar

Hmm. I see the sense in what you're saying. I've tried both of your suggestions above but it's still not working.

Session flash just don't seem to be working properly in my setup. However I have checked the storage/framework/sessions folder and it is writeable and files are appearing in there.

My code to set the flash message is

public function sendContactInfo(ContactUsRequest $request)
{
...
    $request->session()->flash('flash_warning', "Are you sure you are Human?");
    return Redirect::back()->withInput();
}

(ContactUsRequest is a request validator thing which actually works ok)

And in AppServiceProvider boot method

    if (Session::has('flash_warning'))
    {
        View::share('flash_warning', Session::get('flash_warning'));
        die("FW");
    }

And it's not there. I've tried dumping the session (in the actual handler method for this post) with

    echo "<pre>" , print_r( $request->session()->all() ), "</pre>";

And it exists and contains a _flash array which contains empty "old" and "new" arrays and that's it. (For clarity the session contains a token and other bits and bobs but the _flash array is where I assume this should be and that is otherwise empty)

Any thoughts really appreciated!!

lauriek's avatar

This is a timing issue. If I run the session flash checking code in my actual handler method (that the route points to) then it works.

But obviously I don't want to do that, I want it in some common place which runs for every page. I've tried AppServiceProvider but I don't think the session is available there, I've tried originally in my base controller method called by constructor but that's prior to middleware so again the session is not available.

Where should this code now go in L5.5?

mhmd_kavosi's avatar

you can use

if(session()->has('flash_warning'))
{
..
}

and for get session

session('flash_warning');
lauriek's avatar

A ViewComposer seems to have fixed this. I read the docs and pretty much copied the example from the L5.5 docs, injected the Request object to the constructor, and do the checks and view-share in the compose method, and that seems to have resolved the issue.

Please or to participate in this conversation.