Laravel会话数据在重定向到下一页后被清除 [英] Laravel session data get cleared out after redirect to next page
问题描述
使用Laravel 5.2
Using Laravel 5.2
我正在尝试将会话数据存储在控制器中,然后如果验证成功,我想将用户重定向到下一页.当我执行Redirct :: to('nextpage')时,会话数据在我的下一页和控制器中丢失.
Im trying to store session data in controller, then if validation succeed, i want to redirect the user to next page. When i do the Redirct::to('nextpage') the session data is lost in my next page and controller.
这是从'domainSearch'获取表单发布值的控制器
This is the controller in getting the form post value from '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');
}
}
}
这是控制器我正在尝试将会话数据传递到并接听.但是它始终保持为空.
This is the controller im trying to pass session data to and pick up. But it always remain null.
class DomainList extends Controller
{
public function index(Request $request){
var_dump($request->get('domainname'));
return view('domainlist');
}
}
如果我将用户返回到具有返回视图('view')的下一页;会话数据在那里,但是在使用重定向功能时会被清除.
If i send the user to next page with return view('view'); the session data is there, but get cleared out when using the redirect function.
如何重定向而不丢失会话数据和变量?
How to i redirect without losing session data and variables?
推荐答案
尝试在索引中输入var_dump($request->session()->get('domainname'));
.
顺便说一句,您还可以使用session()
全局函数来存储或检索您的会话数据.
BTW, you can also use the session()
global function to store or retrieve your session data.
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']);
});
这篇关于Laravel会话数据在重定向到下一页后被清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!