Laravel 验证登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44889297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Validation login
提问by wenus
This time I'm learning validation. I can't do this for my login form... My code looks like this right now, It works only when I have errors, but this code don't want to login my user. What should be in my code to work and why It doesn't work now? I don't know too how this validate know which email and password should compare to login and from which table? Should I add this info somewhere?
这次我正在学习验证。我无法为我的登录表单执行此操作...我的代码现在看起来像这样,它仅在出现错误时才有效,但此代码不想登录我的用户。我的代码中应该有什么可以工作,为什么它现在不起作用?我也不知道这个验证如何知道哪个电子邮件和密码应该与登录名和哪个表进行比较?我应该在某处添加此信息吗?
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|max:255|email',
'password' => 'required|confirmed',
]);
return redirect()->intended('/panel');
}
回答by rap-2-h
You could consider adding the trait AuthenticatesUsers
to your Controller:
您可以考虑将特征添加AuthenticatesUsers
到您的控制器中:
class LoginController extends Controller {
use \Illuminate\Foundation\Auth\AuthenticatesUsers; // <- add this line
// ...
}
Then you can throw your login
method and everything will work. More info here.
然后你可以抛出你的login
方法,一切都会起作用。更多信息在这里。
But if you want to build your own Authentication system (I don't recommend), you have to dig more. You could have a look here. Then update your controller like this:
但是如果你想建立自己的认证系统(我不推荐),你必须多挖。你可以看看这里。然后像这样更新你的控制器:
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|max:255|email',
'password' => 'required|confirmed',
]);
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Success
return redirect()->intended('/panel');
} else {
// Go back on error (or do what you want)
return redirect()->back();
}
}