溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Laravel怎么自動生成驗證

發(fā)布時間:2021-06-08 15:59:10 來源:億速云 閱讀:150 作者:Leah 欄目:開發(fā)技術

使用Laravel怎么自動生成驗證?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

路由

路由文件中會新加入以下內(nèi)容:

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

首先先是Auth::route();,這句代碼等于以下全部設置(文件位置是\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');
 }
}

而其中并沒有設置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);
  }
  /**
  // 關于 attempt 的介紹可以看我上一篇博客
  protected function attemptLogin(Request $request)
 {
  return $this->guard()->attempt(
   $this->credentials($request), $request->has('remember')
  );
 }
 **/
  // 如果驗證通過的話
  if ($this->attemptLogin($request)) {
   return $this->sendLoginResponse($request);
  }
  // 否則的話增加驗證的統(tǒng)計次數(shù)
  $this->incrementLoginAttempts($request);
  // 返回錯誤信息
  return $this->sendFailedLoginResponse($request);
 }

可以看到驗證的重點還是Auth::attempt()函數(shù),而且默認是使用email進行驗證。

退出操作的代碼如下:

public function logout(Request $request)
 {
  $this->guard()->logout();

  $request->session()->invalidate();

  return redirect('/');
 }

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

protected function guard()
 {
  return Auth::guard();
 }

logout的具體的執(zhí)行代碼如下,別問我怎么找到的,PHPStorm的全項目文本搜索不解釋:\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;
 }

其中牽扯很多,那么我換種角度考慮,假設我們不考慮logout()的具體實現(xiàn),而是思考如何制作自己的退出設置,那么該如何修改源碼呢?好像直接修改成下面的形式就可以了:

public function logout(Request $request)
 {
  Auth::guard()->logout();

  $request->session()->invalidate();
  // 自定義重定向地址
  return redirect('/');
 }

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI