[英]I want to update login with username or email in laravel
I have updated laravel default make:Auth to Login with both username and email. 我已经更新了laravel default make:Auth以使用用户名和电子邮件登录。 I just have override username function in
我只是在其中有覆盖用户名功能
loginController.php to: loginController.php可以:
public function username()
{
$loginType = request()->input('username');
$this->username = filter_var($loginType , FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$this->username => $loginType]);
$this->property_exists($this, 'username') ? $this->username : 'email';
}
but got an error: 但出现错误:
Method [property_exists] does not exist on [App\Http\Controllers\Auth\LoginController].
property_exists
is a php function, not a method from some class. property_exists
是php函数,而不是某些类中的方法。 So, to improve this error, just rewrite your code: 因此,要改善此错误,只需重写代码:
public function username()
{
$loginType = request()->input('username');
$this->username = filter_var($loginType , FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$this->username => $loginType]);
property_exists($this, 'username') ? $this->username : 'email';
}
property_exists — Checks if the object or class has a property
property_exists —检查对象或类是否具有属性
bool property_exists ( mixed $class , string $property )
bool property_exists(混合$ class,字符串$ property)
public function username()
{
$loginType = request()->input('username');
$this->username = filter_var($loginType , FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$this->username => $loginType]);
property_exists($this, 'username') ? $this->username : 'email';
}
The username()
method in the LoginController refers to the username field name sent by the request. LoginController中的
username()
方法引用请求发送的用户名字段名称 。 In your case I would imagine the field name is the same but what changes is whether you use that field to find an email or username. 在您的情况下,我可以想象该字段名称是相同的,但是发生的变化是您是否使用该字段查找电子邮件或用户名。 In that case you should be able to "trick" the user provider to do this by overriding the
credentials
method in your LoginController (and don't override the username
function): 在这种情况下,您应该能够通过覆盖LoginController中的
credentials
方法来“欺骗”用户提供者(而不是覆盖username
功能):
protected function credentials(Request $request)
{
$v = \Validator::make($request->only($this->username()), [
$this->username() => "email"
]);
if ($v->passes()) {
return [
"email" => $request->input($this->username()),
"password" => $request->input("password")
];
} else {
return [
"username" => $request->input($this->username()),
"password" => $request->input("password")
];
}
}
This will be passed to the user provider which will select the user either by the email
column or by the username
column depending on what is passed. 这将被传递给用户提供其要么被选择用户
email
列或由username
取决于什么传递列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.