如何在laravel身份验证中添加额外条件? [英] How can I add extra condition in laravel authentication?
问题描述
我想知道如何添加附加条件,例如laravel附带的laravel身份验证中的active?我可以通过手动进行身份验证来做到这一点,但是如果可以的话,是否可以使用默认身份验证来做到这一点?
I am wondering how can I add extra condition like active in laravel authentication that comes with laravel? I could do that by doing authentication manually but if is there any possibilities to do it with default auth if I can say that ?
推荐答案
如果您覆盖AuthController中AuthenticatesUsers特征的getCredentials
方法,则可以添加其他条件参数:
If you override the getCredentials
method of the AuthenticatesUsers trait in the AuthController, you can add extra conditional parameters:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function getCredentials(Request $request)
{
$credentials = $request->only($this->loginUsername(), 'password');
$credentials['active'] = 'y';
return $credentials;
}
//Other code and methods here
}
此方法将要检查的字段提供给Attempt方法.在文件的开头,您需要使用Illuminate\Http\Request
命名空间才能工作.
This method provides the fields to be checked to the Attempt method. In the beginning of the file, you need to use the Illuminate\Http\Request
namespace to work.
这篇关于如何在laravel身份验证中添加额外条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!