当前位置:首页 > CMS教程 > 其它CMS > 列表

Laravel 自动生成验证的实例讲解:login / logout

发布:smiling 来源: PHP粉丝网  添加日期:2022-01-02 13:53:57 浏览: 评论:0 

今天小编就为大家分享一篇Laravel 自动生成验证的实例分析:login / logout,具有好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

Laravel 自动授权讲解

看到这部分文档,经常看见的一句话就是php artisan make:auth,经常好奇这段代码到底干了什么,现在就来扒一扒。

路由

路由文件中会新加入以下内容:

Auth::routes();

Route::get('/home','HomeController@index')->name('home');

首先先是Auth::route();,这句代码等于以下全部设置(文件位置是\Illuminate\Routing\Router.php):

  1. /** 
  2.   * Register the typical authentication routes for an application. 
  3.   * 
  4.   * @return void 
  5.   */ 
  6.  public function auth() 
  7.  { 
  8.   // Authentication Routes... 
  9.   $this->get('login''Auth\LoginController@showLoginForm')->name('login'); 
  10.   $this->post('login''Auth\LoginController@login'); 
  11.   $this->post('logout''Auth\LoginController@logout')->name('logout'); 
  12.  
  13.   // Registration Routes... 
  14.   $this->get('register''Auth\RegisterController@showRegistrationForm')->name('register'); 
  15.   $this->post('register''Auth\RegisterController@register'); 
  16.  
  17.   // Password Reset Routes... 
  18.   $this->get('password/reset''Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request'); 
  19.   $this->post('password/email''Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email'); 
  20.   $this->get('password/reset/{token}''Auth\ResetPasswordController@showResetForm')->name('password.reset'); 
  21.   $this->post('password/reset''Auth\ResetPasswordController@reset'); 
  22.  } 

这一部分先讲注册,首先,可以看到登录(login)的路由指向的是Auth\LoginController@showLoginForm,这个控制器是app\Http\Auth\LoginController.php,这里贴一下他的代码:

  1. class LoginController extends Controller 
  2.  /* 
  3.  |-------------------------------------------------------------------------- 
  4.  | Login Controller 
  5.  |-------------------------------------------------------------------------- 
  6.  | 
  7.  | This controller handles authenticating users for the application and 
  8.  | redirecting them to your home screen. The controller uses a trait 
  9.  | to conveniently provide its functionality to your applications. 
  10.  | 
  11.  */ 
  12.  
  13.  use AuthenticatesUsers; 
  14.  
  15.  /** 
  16.   * Where to redirect users after login. 
  17.   * 
  18.   * @var string 
  19.   */ 
  20.  protected $redirectTo = '/home'
  21.  
  22.  /** 
  23.   * Create a new controller instance. 
  24.   * 
  25.   * @return void 
  26.   */ 
  27.  public function __construct() 
  28.  { 
  29.   $this->middleware('guest')->except('logout'); 
  30.  } 

而其中并没有设置showLoginForm方法,该方法被保存在trait AuthenticatesUsers中,该方法的代码如下:

  1. public function showLoginForm() 
  2.  { 
  3.   return view('auth.login'); 
  4.  } 

就是返回一个视图,下面我们来看该视图:

<form class="form-horizontal" method="POST" action="{{ route('login') }}">

</form>

而其中最重要的就是看这个表单被提交到了哪里,结合上面的路由表,可以看到是

  1. public function login(Request $request
  2.  { 
  3.   $this->validateLogin($request); 
  4.   /** 
  5.   * 
  6.   protected function validateLogin(Request $request) 
  7.  { 
  8.   $this->validate($request, [ 
  9.    $this->username() => 'required|string', 
  10.    'password' => 'required|string', 
  11.   ]); 
  12.  } 
  13.   其中 $this->username() 就是 return 'email'; 
  14.   **/ 
  15.   // 限制请求次数,防止暴力破解的 
  16.   if ($this->hasTooManyLoginAttempts($request)) { 
  17.    $this->fireLockoutEvent($request); 
  18.  
  19.    return $this->sendLockoutResponse($request); 
  20.   } 
  21.   /** 
  22.   // 关于 attempt 的介绍可以看我上一篇博客 
  23.   protected function attemptLogin(Request $request) 
  24.  { 
  25.   return $this->guard()->attempt( 
  26.    $this->credentials($request), $request->has('remember') 
  27.   ); 
  28.  } 
  29.  **/ 
  30.   // 如果验证通过的话 
  31.   if ($this->attemptLogin($request)) { 
  32.    return $this->sendLoginResponse($request); 
  33.   } 
  34.   // 否则的话增加验证的统计次数 
  35.   $this->incrementLoginAttempts($request); 
  36.   // 返回错误信息 
  37.   return $this->sendFailedLoginResponse($request); 
  38.  } 

可以看到验证的重点还是Auth::attempt()函数,而且默认是使用email进行验证。

退出操作的代码如下:

  1. public function logout(Request $request
  2.  { 
  3.   $this->guard()->logout(); 
  4.  
  5.   $request->session()->invalidate(); 
  6.  
  7.   return redirect('/'); 
  8.  } 

$this->guard()的代码如下:

  1. protected function guard() 
  2.  { 
  3.   return Auth::guard(); 
  4.  } 

logout的具体的执行代码如下,别问我怎么找到的,PHPStorm的全项目文本搜索不解释:\Illuminate\Auth\SessionGuard.php:

  1. public function logout() 
  2.  { 
  3.   $user = $this->user(); 
  4.  
  5.   $this->clearUserDataFromStorage(); 
  6.  
  7.   if (! is_null($this->user)) { 
  8.    $this->cycleRememberToken($user); 
  9.   } 
  10.  
  11.   if (isset($this->events)) { 
  12.    $this->events->dispatch(new Events\Logout($user)); 
  13.   } 
  14.  
  15.   // Once we have fired the logout event we will clear the users out of memory 
  16.   // so they are no longer available as the user is no longer considered as 
  17.   // being signed into this application and should not be available here. 
  18.   $this->user = null; 
  19.  
  20.   $this->loggedOut = true; 
  21.  } 

其中牵扯很多,那么我换种角度考虑,假设我们不考虑logout()的具体实现,而是思考如何制作自己的退出设置,那么该如何修改源码呢?好像直接修改成下面的形式就可以了:

  1. public function logout(Request $request
  2.  { 
  3.   Auth::guard()->logout(); 
  4.  
  5.   $request->session()->invalidate(); 
  6.   // 自定义重定向地址 
  7.   return redirect('/'); 
  8.  } 

其中的很多内容都跟我们的设置无关,全自动的调用,所以我们的退出按钮就只需要运行上述代码即可。本人请测有效。

Tags: Laravel自动生成验证 login logout

分享到: