jeudi 25 juin 2020

Laravel - Working with sessions between controllers

I need to show some values in all app, and sometimes I need to use these values between controllers. I was trying to use Session, but I maybe it is not the answer to that, because if I am not wrong, Laravel store the session data temporarily in memory and only after everything has been executed, will it dump the session data into storage (file, cookie, redis, whatever).

So for example, I have a controller like that:

class GiftController extends Controller
{
    public function dashboard(){
        $token = (Session::has('token')) ? Session::get('token') : NULL;
        if (is_null($token)){
           return view('test.erro', ['message' => 'no session!']);
        }else{
           return view('test.ok', ['message' => $token]);
        } 
    }


    public function setsession(){
          Session::put('token','xxxxxxxxxxxx');
          return redirect('test/dashboard');  
    }
}

In this case, if I do an echo on Session::get('token') into the setsession(), is showing the value, but when it goes to the dashboard(), the session shows nothing.

What I am doing wrong?
or What is the better Idea to use instead off session?

I am using the Laravel Framework 5.8.37



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire