Laravel 5使用用户名而不是电子邮件登录

时间:2015-08-06 09:42:19

标签: php laravel

如何使用用户名登录,而不是使用电子邮件? Email

这是我的login.blade.php

@extends('layouts.main')
@section('errors')
@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
@section('content')
<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>
</body>
@stop

第二个问题:如何重置密码?在此先感谢;)

2 个答案:

答案 0 :(得分:2)

您将电子邮件更改为您表单中的用户名

<form method="POST" action="/auth/login">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="text" name="username" value="{{ old('username') }}">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>

并且在您的AuthenticatesAndRegisterUsers.php中,您也将电子邮件更改为用户名。

public function postLogin(Request $request)
    {
        $this->validate($request, [
            'username' => 'required', 'password' => 'required',
        ]);

        $credentials = $request->only('username', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
        {
            return redirect()->intended($this->redirectPath());
        }

        return redirect($this->loginPath())
                    ->withInput($request->only('username', 'remember'))
                    ->withErrors([
                        'username' => $this->getFailedLoginMessage(),
                    ]);
    }

答案 1 :(得分:0)

默认情况下,Laravel uses the email field for authentication。如果您想对此进行自定义,则可以在LoginController上定义一个用户名方法:

public function username()
{
    return 'username';
}