Laravel 自動授權(quán)講解
看到這部分文檔,經(jīng)??匆姷囊痪湓捑褪莗hp artisan make:auth,經(jīng)常好奇這段代碼到底干了什么,現(xiàn)在就來扒一扒。
路由
路由文件中會新加入以下內(nèi)容:
Auth::routes();
Route::get('/home','HomeController@index')->name('home');
首先先是Auth::route();,這句代碼等于以下全部設(shè)置(文件位置是\Illuminate\Routing\Router.php):
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
}
這一部分先講注冊,首先,可以看到登錄(login)的路由指向的是Auth\LoginController@showLoginForm,這個控制器是app\Http\Auth\LoginController.php,這里貼一下他的代碼:
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 = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
而其中并沒有設(shè)置showLoginForm方法,該方法被保存在trait AuthenticatesUsers中,該方法的代碼如下:
public function showLoginForm()
{
return view('auth.login');
}
就是返回一個視圖,下面我們來看該視圖:
form class="form-horizontal" method="POST" action="{{ route('login') }}">
/form>
而其中最重要的就是看這個表單被提交到了哪里,結(jié)合上面的路由表,可以看到是
public function login(Request $request)
{
$this->validateLogin($request);
/**
*
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string',
'password' => 'required|string',
]);
}
其中 $this->username() 就是 return 'email';
**/
// 限制請求次數(shù),防止暴力破解的
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
/**
// 關(guān)于 attempt 的介紹可以看我上一篇博客
protected function attemptLogin(Request $request)
{
return $this->guard()->attempt(
$this->credentials($request), $request->has('remember')
);
}
**/
// 如果驗(yàn)證通過的話
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
// 否則的話增加驗(yàn)證的統(tǒng)計次數(shù)
$this->incrementLoginAttempts($request);
// 返回錯誤信息
return $this->sendFailedLoginResponse($request);
}
可以看到驗(yàn)證的重點(diǎn)還是Auth::attempt()函數(shù),而且默認(rèn)是使用email進(jìn)行驗(yàn)證。
退出操作的代碼如下:
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/');
}
$this->guard()的代碼如下:
protected function guard()
{
return Auth::guard();
}
logout的具體的執(zhí)行代碼如下,別問我怎么找到的,PHPStorm的全項(xiàng)目文本搜索不解釋:\Illuminate\Auth\SessionGuard.php:
public function logout()
{
$user = $this->user();
$this->clearUserDataFromStorage();
if (! is_null($this->user)) {
$this->cycleRememberToken($user);
}
if (isset($this->events)) {
$this->events->dispatch(new Events\Logout($user));
}
// Once we have fired the logout event we will clear the users out of memory
// so they are no longer available as the user is no longer considered as
// being signed into this application and should not be available here.
$this->user = null;
$this->loggedOut = true;
}
其中牽扯很多,那么我換種角度考慮,假設(shè)我們不考慮logout()的具體實(shí)現(xiàn),而是思考如何制作自己的退出設(shè)置,那么該如何修改源碼呢?好像直接修改成下面的形式就可以了:
public function logout(Request $request)
{
Auth::guard()->logout();
$request->session()->invalidate();
// 自定義重定向地址
return redirect('/');
}
其中的很多內(nèi)容都跟我們的設(shè)置無關(guān),全自動的調(diào)用,所以我們的退出按鈕就只需要運(yùn)行上述代碼即可。本人請測有效。
以上這篇Laravel 自動生成驗(yàn)證的實(shí)例分析:login / logout就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- laravel-admin自動生成模塊,及相關(guān)基礎(chǔ)配置方法
- laravel批量生成假數(shù)據(jù)的方法
- Laravel 自定命令以及生成文件的例子
- Laravel自動生成UUID,從建表到使用詳解