使用Laravel怎么自动生成验证
创始人
2024-12-06 09:01:35
0次

路由

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

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');
 }

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


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

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';
  **/
  // 限制请求次数,防止暴力破解的
  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);
  }
  // 否则的话增加验证的统计次数
  $this->incrementLoginAttempts($request);
  // 返回错误信息
  return $this->sendFailedLoginResponse($request);
 }

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

退出操作的代码如下:

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

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

  return redirect('/');
 }

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

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

logout的具体的执行代码如下,别问我怎么找到的,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()的具体实现,而是思考如何制作自己的退出设置,那么该如何修改源码呢?好像直接修改成下面的形式就可以了:

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

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

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...