这是我对用户名或电子邮件登录的看法:
$input = Input::all();
$remember = (isset($input['remember'])) ? true : null;
$rules = array('email_or_username' => 'required', 'password' => 'required');
$validator = Validator::make($input, $rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
// get model based on username_or_email, returns null if not present
$user = User::where('email', $input['email_or_username'])->orWhere('username', $input['email_or_username'])->first();
if(!$user) {
$attempt = false;
} else {
$attempt = Auth::attempt(array('email' => $user->email, 'password' => $input['password']),$remember);
}
if($attempt) {
return Redirect::intended('/')->with(array('flash_message' => 'Successfully logged into ' . Lang::get('site.general.title') . '!', 'flash_type' => 'success') );
}
return Redirect::back()->with(array('flash_message' => 'Invalid credentials, please try again', 'flash_type' =>'danger'))->withInput();