当用户输入他的登录用户名和密码来存储他的详细信息并将其重定向到另一个页面时,我是否可以查询数据库以获取他之前提供的附加信息?请考虑以下情况:
这是我到目前为止所拥有的
public function login() {
return View::make('site.users.login');
}
public function loginSubmit() {
$validatorRules = array(
'username' => 'required|alpha_dash',
'password' => 'required|min:6'
);
Input::merge(array_map('trim', Input::all()));
$validator = Validator::make(Input::all(), $validatorRules);
if ($validator->fails()) {
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
$user = User::where('username', Input::get('username'))->first();
if (!$user) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
if (!Hash::check(Input::get('password'), $user->password)) {
$validator->messages()->add('username', 'Invalid login or password.');
return Redirect::to('/users/login')->withErrors($validator->errors())->withInput(Input::except(['captcha']));
}
Session::put('user', ['user_id' => $user->user_id]);
return Redirect::to('/users/auth/' . $user->user_id . '?_token=' . csrf_token())->with('message_success', '.');
}
public function loginAuth() {
Session::get('user', ['user_id' => $user->user_id]);
$key = DB::table('users')->select('key')->where('user_id', '=', $data->user_id)->first();
return View::make('site.users.auth', [
'key' => $key
]);
}
还是有另一种方法可以做到这一点?这部分来源给出了简单的错误
production.ERROR: exception 'ErrorException' with message 'Undefined variable: user'
会话获取功能
Session::get('user', ['user_id' => $user->user_id]);
答案 0 :(得分:1)
您可以像这样修复代码:
public function loginAuth()
{
$data = Session::get('user');
$key = DB::table('users')->select('key')->where('user_id', '=', $data->user_id)->first();
return View::make('site.users.auth', [
'key' => $key
]);
}