简体   繁体   English

Laravel - 使用电话和用户名登录,FILTER_VALIDATE_EMAIL 问题

[英]Laravel - login with phone and username, FILTER_VALIDATE_EMAIL ISSUE

I want my project to have two ways of login, with phone or with username = korisnicko_ime .我希望我的项目有两种登录方式,使用phone或使用username = korisnicko_ime This is what I did in LoginController :这是我在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;
    }

And I also changed public function username() in AuthenticatesUsers trait like this:我还在AuthenticatesUsers trait 中更改了public function username() ,如下所示:

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

And my blade looks like this:我的刀片看起来像这样:

<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>

The problem is in this part of code:问题出在这部分代码中:

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

if I change email to phone and erase FILTER_VALIDATE_EMAIL then I can only login with my phone and not with korisnicko_ime .如果我将email更改为phone并删除FILTER_VALIDATE_EMAIL那么我只能使用我的phone登录,而不能使用korisnicko_ime What should I do?我该怎么办?

How about this:这个怎么样:

Basically, you need a way to tell the input whether is a phone number or a username, here is an example to check the input is a phone number, it's very simple you may need to modify for your specific need.基本上,您需要一种方法来告诉输入是电话号码还是用户名,这是一个检查输入是电话号码的示例,这很简单,您可能需要根据您的特定需要进行修改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM