Session variable doesn't persist during redire

2019-08-13 22:13发布

I am making a redirect to homecontroller after user authenticates and set a auth token to session for use in the home controller as -

Controller 1

$secret = Crypt::encrypt($secret);
  Session::put('secret', $secret);
  Session::save();      
  return redirect()->action('loginController@homeRedirect');

Controller 2 -> homeRedirect

function homeRedirect(){
      dd(Session::all());
      if(Session::has('secret')){
        $secret = Session::get('secret');
        Session::forget('secret');

Here the dump comes blank array. nothing comes in the session, however if i test this dd(Session::all()) then it gives correct details.

Also I have tried as sending data using with('secret', $secret) but that is also returning a blank session.

My session.php file has following entry 'driver' => 'file', and the laravel version is 5.2.

EDIT

Also if I try like this -

function homeRedirect(Request $request){
  $ses = $request->session()->get('secret');
  dd($ses);

Then I get error stating Session store not set on request.

1条回答
Anthone
2楼-- · 2019-08-13 22:42

As you are using Laravel 5.2 , you should make sure you have set route for your loginController@homeRedirect in web middleware.

Your route should be defined in routes.php like so

Route::group(['middleware' => ['web']], function () {

    Route::get('/homeredirect','loginController@homeRedirect');
}

If it's defined this way:

Route::group(['middleware' => ['web']], function () {


}

Route::get('/homeredirect','loginController@homeRedirect');

it won't work

查看更多
登录 后发表回答