Laravel 4 Auth::attempt using either email or username and password(Laravel 4 Auth::attempt 使用电子邮件或用户名和密码)
问题描述
在 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.
我想用不同的方式来做,比如我的表中有 email、username 和 password.$usernameinput 可以是我表中的 email 或 username 字段.
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 条件如下:
How can I Auth::attempt with a condition like:
(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)) {
// ...
}
另一种选择是扩展 IlluminateAuthGuard 添加所需的功能并将其设置为 auth 组件,在一个新的服务提供者中.
The other option is to extend IlluminateAuthGuard adding the wanted functionality and setting it as the auth component, in a new service provider.
这篇关于Laravel 4 Auth::attempt 使用电子邮件或用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 4 Auth::attempt 使用电子邮件或用户名和密码
基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
