Laravel - 使用 phone 和用户名登录,FILTER_VALIDATE_EMAIL ISSUE

Laravel - login with phone and username, FILTER_VALIDATE_EMAIL ISSUE

我希望我的项目有两种登录方式,phoneusername = korisnicko_ime。 这就是我在 LoginController:

中所做的
     /**
     * Login username to be used by the controller.
     *
     * @var string
     */
    protected $username;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
        $this->username = $this->findUsername();
    }

    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function findUsername()
    {
        $login = request()->input('login');
 
        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'korisnicko_ime';
 
        request()->merge([$fieldType => $login]);
 
        return $fieldType;
    }
 
    /**
     * Get username property.
     *
     * @return string
     */
    public function username()
    {
        return $this->username;
    }

而且我还在 AuthenticatesUsers 特征中更改了 public function username(),如下所示:

     /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'phone';
    }

我的 blade 看起来像这样:

<div class="form-group row">
   <label for="login" class="col-sm-4 col-form-label text-md-right">
      {{ __('Username or Phone') }}
   </label>

   <div class="col-md-6">
      <input id="login" type="text" class="form-control{{ $errors->has('korisnicko_ime') || $errors->has('phone') ? ' is-invalid' : '' }}" name="login" value="{{ old('korisnicko_ime') ?: old('phone') }}" required autofocus>

      @if ($errors->has('korisnicko_ime') || $errors->has('phone'))
      <span class="invalid-feedback">
      <strong>{{ $errors->first('korisnicko_ime') ?: $errors->first('phone') }}</strong>
      </span>
      @endif
   </div>
</div>

问题出在这部分代码:

$fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'korisnicko_ime';

如果我将 email 更改为 phone 并删除 FILTER_VALIDATE_EMAIL,那么我只能使用 phone 而不是 korisnicko_ime 登录。 我该怎么办?

这个怎么样:

基本上,您需要一种方法来判断输入是 phone 数字还是用户名,这里有一个示例来检查输入是 phone 数字,这很简单,您可以需要根据您的具体需要进行修改。

$fieldType = ctype_digit($login) ? 'phone' : 'korisnicko_ime';