侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Laravel  /  管理员 发布于 9年前   204

Laravel 自动授权讲解

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

路由

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

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

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

以上这篇Laravel 自动生成验证的实例分析:login / logout就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:

  • laravel-admin自动生成模块,及相关基础配置方法
  • laravel批量生成假数据的方法
  • Laravel 自定命令以及生成文件的例子
  • Laravel自动生成UUID,从建表到使用详解


  • 上一条:
    解决Laravel5.2 Auth认证退出失效的问题
    下一条:
    Laravel 登录后清空COOKIE的操作方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • Laravel 11.14版本发布 - 新的字符串助手和ServeCommand改进(0个评论)
    • Laravel 11.12版本发布 - Artisan的`make`命令自动剪切`.php `扩展(0个评论)
    • Laravel的轻量型购物车扩展包:binafy/laravel-cart(37个评论)
    • Laravel 11.11版本发布 - 查看模型中的第三方关系:show(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-01
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客