【问题标题】:Session is always empty after redirect重定向后会话始终为空
【发布时间】:2019-09-24 17:28:47
【问题描述】:

我正在使用此代码重定向到闪现会话的路由:

return redirect()->route('test.createInstitution')->with([
    'gates_response' => $response
]);

我正在尝试像这样在控制器中获取这个闪现的会话:

$response = session('gates_response');

但无论如何,我总是得到空值。甚至尝试过不闪现会话(使用session(['gates_response' => $response]);)但相同...

如果我在 web.php 中使用,我可以成功访问会话。

Route::get('/test', function() {
    ...
    return redirect()->route('test.createInstitution')->with([
        'gates_response' => $response
    ]);
});

路线:

Route::get('/test/register/{institution?}', 'test@redirect')->name('test.register');
Route::post('/test/callback/{institution?}', 'test@callback')->name('test.callback');
Route::get('/test/create-institution', 'test@createInstitution')->name('test.createInstitution');
Route::post('/test/store-institution', 'test@storeInstitution')->name('test.storeInstitution');

任何想法如何解决这个问题?提前感谢您的回答!

P.S Redirect 方法是从 /test/callback/institution URI 调用的

【问题讨论】:

  • 抱歉,您在 laravel 设置中设置了哪些会话存储?如果是 redis 或其他会话存储,请检查是否启用了 redis :)
  • 会话驱动是文件
  • 检查 storage/framework/sessions 文件夹的权限,或者尝试将会话驱动更改为数组

标签: laravel


【解决方案1】:

先检查session是否有数据..

 if(Session::has('gates_response'))
 {
   $session_data = Session::get('gates_response');
 }

更改重定向代码。不需要数组括号[

 Session::put('gates_response',$response);
 Session::save();
 return redirect()->route('evartai.createInstitution');

【讨论】:

  • 正如我提到的,我总是得到 null
  • 检查$response变量中是否有数据
  • 尝试更改重定向语句并检查
  • 更改后仍然为空
  • 然后在重定向之前,使用Session::put('gates_response',$response) 将值放入会话中并简单地重定向return redirect()->route('evartai.createInstitution')
猜你喜欢
  • 2021-02-22
  • 2016-06-07
  • 2017-11-02
  • 2015-10-07
  • 2017-03-31
  • 1970-01-01
  • 2017-01-21
  • 2012-07-30
  • 1970-01-01
相关资源
最近更新 更多