Laravel 4 Auth ::尝试使用电子邮件或用户名和密码 [英] Laravel 4 Auth::attempt using either email or username and password

查看:100
本文介绍了Laravel 4 Auth ::尝试使用电子邮件或用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel中尝试登录时,我通常使用类似这样的内容:

In laravel for login attempt I generally use something like this:

if (Auth::attempt(array('email' => $usernameinput, 'password' => $password), true))
{
    // The user is being remembered...
}

基本上$usernameinput将从我的表中检查email.

Basically $usernameinput will check with email from my table.

我正在考虑以不同的方式进行操作,例如表中有emailusernamepassword. $usernameinput可以是表中的emailusername字段.

I was thinking to do it in a different way, like there is email, username and password in my table. $usernameinput can be either email or username field in my table.

如何在以下情况下Auth::attempt:

(email==$usernameinput OR username==$usernameinput) AND password == $password

推荐答案

好吧,您可以简单地检查$usernameinput是否与电子邮件模式匹配,如果匹配,则使用email字段,否则使用username字段.这听起来是合理的,因为,好吧,您的数据库中实际上不应该包含任何不匹配该模式的电子邮件.像这样:

Well, you could simply check if $usernameinput matches an email pattern and if it does, use the email field, else use the username field. This sounds reasonable because, well, you really shouldn't have any e-mail in your database that doesn't match the pattern. Something like this:

$field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

if (Auth::attempt([$field => $usernameinput, 'password' => $password], true)) {
    // ...
}

另一种选择是扩展Illuminate\Auth\Guard,以添加所需的功能并将其设置为新服务提供商中的auth组件.

The other option is to extend Illuminate\Auth\Guard adding the wanted functionality and setting it as the auth component, in a new service provider.

这篇关于Laravel 4 Auth ::尝试使用电子邮件或用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆