[英]Laravel session data get cleared out after redirect to next page
使用Laravel 5.2
我試圖將會話數據存儲在控制器中,然后如果驗證成功,我想將用戶重定向到下一頁。 當我執行Redirct :: to('nextpage')時,會話數據在我的下一頁和控制器中丟失。
這是從'domainSearch'獲取表單發布值的控制器
class domainSearch extends Controller
{
public function store(Request $request)
{
// validate the info, create rules for the inputs
$rules = array(
'domainSearch' => 'required' // make sure the username field is not empty
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('/')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
}
else {
// validation not successful, send back to form
$request->session()->put('domainname', $request->get('domainSearch'));
return Redirect::to('/domainlist');
}
}
}
這是我試圖將會話數據傳遞到並接收的控制器。 但它始終保持為空。
class DomainList extends Controller
{
public function index(Request $request){
var_dump($request->get('domainname'));
return view('domainlist');
}
}
如果我將用戶發送到下一頁並帶有返回視圖('view'); 會話數據在那里,但在使用重定向功能時被清除。
如何在不丟失會話數據和變量的情況下重定向?
嘗試var_dump($request->session()->get('domainname'));
在你的索引中。
順便說一句,您還可以使用session()
全局函數來存儲或檢索會話數據。
Route::get('home', function () {
// Retrieve a piece of data from the session...
$value = session('key');
// Store a piece of data in the session...
session(['key' => 'value']);
});
這是因為您嘗試檢索GET參數而不是SESSION變量。 嘗試這個:
class DomainList extends Controller
{
public function index(Request $request){
var_dump($request->session()->get('domainname'));
return view('domainlist');
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.