我仍在学习Laravel,因此我在Google上搜索了一些可以帮助我的代码,但找不到完整的代码。我汇总了发现的内容并得到了:(不起作用)
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
protected $username;
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->username = $this->findUsername();
}
public function findUsername()
{
$login = request()->input('username');
$fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$fieldType => $login]);
return $fieldType;
}
/**
* Get username property.
*
* @return string
*/
public function username()
{
return $this->username;
}
protected function sendFailedLoginResponse(Request $request)
{
if (Auth::check())
{
return redirect()->intended('home');
}
else
{
return redirect()->back()->withErrors([
'password' => 'Incorrect username or password'
]);
}
}
}
我的目标是使用用户名或电子邮件登录,如果凭据错误则发送错误。 我做了第一部分(当时我还没有使用最后一个功能),但是当我尝试添加第二个功能时,我把它弄坏了:)) 我没有收到错误,只是让我停留在登录页面上