Laravel flash or session messages not expiring [ n

2020-04-10 07:50发布

问题:

Updated after some research
After some research I conclude that, my sessions are not maintained until I save them explicitly, below code works well, but WHY???? Ref here

Session::put('lets_test', 2);
Session::save();

Old Question
I'm new to laravel 5.3, and stuck at a problem. My Laravel Session or Flash messages are not expiring they display each time page is reloaded, until I use Session::flush() Below is my controller code

<?php

namespace App\Http\Controllers;


use Session;
use Auth;
use Illuminate\Http\Request;
use App\User;
use App\Hospital;
use App\Wpr;
use Helper;

class OperatorController extends Controller
{
    public $user_detail;

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('operator');
    }
    public function store (Request $request){ //Form is being submitted here
        //My logic here
        Session::flash('user_message', 'Thank You');
        return redirect('/operator/wpr');
    }
}

I've also used Session::set('user_message', 4); and blade view

@if(Session::has('user_message'))
    <div class="message  animated tada">
        {{ Session::get('user_message') }}
    </div>
@endif

I've tried with Session::forget('user_message') but no luck. Updating my post after some research. I've got somewhat close to my problem by reading this post on stack because this question is exactly same to my problem but unfortunately it still persists, I've changed my session storage from file to database (in case file permissions to storage directory). What might be other possibilities?

Please help, thanks in advance.

回答1:

Ultimately, somehow, I managed to find solution for my problem, of not sustaining sessions. I don't think its any issue related to file permission. Now I'm saving my session explicitly and removing it explicitly. Made a Helper class added two methods

public static function SetMessage($message, $type){ 
   Session::put('user_message', $message);
   Session::put('user_message_type', $type);
   Session::save();
}
public static function ForgetMessage(){ 
   Session::forget('user_message');
   Session::forget('user_message_type');
   Session::save();
}

and within Controller class

 Helper::SetMessage('Record updated successfully', 'success');

and within blade view tempalte

@if(Session::has('user_message'))
    <div class="alert alert-{{ Session::get('user_message_type') }}">
     {{ Session::get('user_message') }}
     {{ Helper::ForgetMessage('user_message') }}
    </div>
@endif

I hope this might help someone who is facing such kind problem. But why is it so, still unknown, maybe one day I'll post the reason too. More suggestions are welcomed, if this could be done in a better way.



回答2:

try this:

if(isset($_SESSION['user_message'])) {
        $message = $_SESSION['user_message'];
        unset($_SESSION['user_message']);
        return $message;
    }


回答3:

In your (Laravel) blade add $request->session()->forget('key'); or Session::forget('key'); at the end of if loop this will end or delete the session of that key. This may help you for more reference about session of laravel 5.3 visit laravel 5.3