Laravel 5.5 无法使用身份验证脚手架登录
Posted
技术标签:
【中文标题】Laravel 5.5 无法使用身份验证脚手架登录【英文标题】:Laravel 5.5 cannot login using auth scaffolding 【发布时间】:2018-12-16 03:51:48 【问题描述】:在安装身份验证脚手架并创建数据库以存储仅使用用户名和密码的凭据后,我正在我的应用程序上进行注册 - 登录功能。我已成功注册新帐户,存储到数据库中,并重定向到管理页面。但是注销后,我无法使用以前创建的帐户登录,并且显示这样的错误消息..
Error message that prevent login
我该怎么办?
这是我的:
web.php
Route::get('/', 'FrontController@home')->name('home');
Route::get('/login', 'FrontController@login')->name('login');
Auth::routes();
Route::get('/home', 'AdminController@index')->name('admin');
Route::get('/manual', 'ManualLoginController@manual')->name('manual');
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout');
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('guest')->except('logout');
public function logout(Request $request)
Auth::logout();
return redirect('/login');
login.blade.php
<div class="container-fluid-full">
<div class="row-fluid">
<div class="login-box">
<div class="icons">
<a href="/"><i class="halflings-icon home"></i></a>
<a href="#"><i class="halflings-icon cog"></i></a>
</div>
<h2 style="font-family: 'Raleway'; font-weight: 800">Masuk sebagai administrator</h2>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li> $error </li>
@endforeach
</ul>
</div>
@endif
<form class="form-horizontal" action="route('login')" method="POST">
@csrf
<fieldset>
<div class="input-prepend" title="Username">
<label for="username"> __('Username') </label>
<span class="add-on"><i class="halflings-icon user"></i></span>
<input class="input-large span10 form-control $errors->has('username') ? ' is-invalid' : '' " name="username" id="username" type="text" placeholder="username" value=" old('username') " required autofocus/>
@if ($errors->has('username'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('username') </strong>
</span>
@endif
</div>
<div class="clearfix"></div>
<div class="input-prepend" title="Password">
<label for="password"> __('Password') </label>
<span class="add-on"><i class="halflings-icon lock"></i></span>
<input class="input-large span10 form-control $errors->has('password') ? ' is-invalid' : '' " name="password" id="password" type="password" placeholder="Password" required/>
@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('password') </strong>
</span>
@endif
</div>
<div class="clearfix"></div>
<div class="button-login">
<button type="submit" class="btn btn-primary"> __('Login')
</button>
</div>
<div class="clearfix"></div>
</fieldset>
</form>
</div><!--/span-->
</div><!--/row-->
</div>
RegisterController.php
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('guest');
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
return Validator::make($data, [
'username' => 'required|string|max:255|unique:users',
'password' => 'required|string|min:4|confirmed',
]);
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
return User::create([
'username' => $data['username'],
'password' => Hash::make($data['password']),
]);
FronController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FrontController extends Controller
public function home ()
return view ('home');
public function login ()
return view ('login');
AdminController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('auth');
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
return view('admin');
【问题讨论】:
【参考方案1】:默认情况下,laravel 使用email
字段进行身份验证,您可以通过在登录控制器中定义以下函数来覆盖它
public function username()
return 'username';
详情可以查看Illuminate\Foundation\Auth\AuthenticatesUsers
trait
【讨论】:
哇哦,你是我的救星!谢啦兄弟! :D以上是关于Laravel 5.5 无法使用身份验证脚手架登录的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 默认使用 Vuejs 进行身份验证,仅在登录后提供 spa