Ad

How To Set Session In One Method And Get The Session Data In Another Method In Laravel 5.2

This function will get the session value

public function getSession(){
    $value =Session::get('email');
    var_dump($value) ;
}

this function use to set the session values

public function setSession(){
    Session::put('email', 'aaaaaaa.com');

    $value=Session::get('email');
    var_dump($value);

}

routes.php

Route::get('store','[email protected]');
Route::get('display','[email protected]');

when type display in url it's show NULL

Ad

Answer

As you use Laravel 5.2 probably you have to set your routes under the 'web' group to be able to use the session.

In fact, Laravel initializes the Session in the \Illuminate\Session\Middleware\StartSession::class middleware, that is grouped under the 'web' middleware group ( as you can see from the Kernel.php file )

So, to use the session, your routes should use that middlware:

Route::group(['middleware' => ['web']], function () {
    Route::get('store','[email protected]');
    Route::get('display','[email protected]');
});

Besides, i suggest you to close the request cycle properly, to be sure that the session will be set, for example:

public function setSession(){
    Session::put('email', 'aaaaaaa.com');
    $value=Session::get('email');

    //this will end the request-lifecycle 
    return Response::json(['result' => 'ok']);
}
Ad
source: stackoverflow.com
Ad