Laravel flash or session messages not expiring [ not maintained Updated ]

六眼飞鱼酱① 提交于 2019-12-01 23:55:55

问题


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



来源:https://stackoverflow.com/questions/41690759/laravel-flash-or-session-messages-not-expiring-not-maintained-updated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!