Laravel 自动生成验证的实例讲解:login / logout
Laravel 自动授权讲解
看到这部分文档,经常看见的一句话就是php artisan make:auth,经常好奇这段代码到底干了什么,现在就来扒一扒。
路由
路由文件中会新加入以下内容:
Auth::routes();
Route::get('/home','HomeController@index')- name('home');
首先先是Auth::route();,这句代码等于以下全部设置(文件位置是IlluminateRoutingRouter.php):
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
$this- get('login', 'AuthLoginController@showLoginForm')- name('login');
$this- post('login', 'AuthLoginController@login');
$this- post('logout', 'AuthLoginController@logout')- name('logout');
// Registration Routes...
$this- get('register', 'AuthRegisterController@showRegistrationForm')- name('register');
$this- post('register', 'AuthRegisterController@register');
// Password Reset Routes...
$this- get('password/reset', 'AuthForgotPasswordController@showLinkRequestForm')- name('password.request');
$this- post('password/email', 'AuthForgotPasswordController@sendResetLinkEmail')- name('password.email');
$this- get('password/reset/{token}', 'AuthResetPasswordController@showResetForm')- name('password.reset');
$this- post('password/reset', 'AuthResetPasswordController@reset');
}
这一部分先讲注册,首先,可以看到登录(login)的路由指向的是AuthLoginController@showLoginForm,这个控制器是appHttpAuthLoginController.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
而其中最重要的就是看这个表单被提交到了哪里,结合上面的路由表,可以看到是
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的全项目文本搜索不解释:IlluminateAuthSessionGuard.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 EventsLogout($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就是小编分享给大家的全部内容了,希望能给大家一个参考。
- Elasticsearch聚合 之 Range区间聚合
- Elasticsearch集群管理
- 图m着色问题
- Elasticsearch入门介绍
- Elasticsearch聚合 之 Histogram 直方图聚合
- Elasticsearch聚合 之 Date Histogram聚合
- Elasticsearch聚合 之 Terms
- Elasticsearch聚合初探——metric篇
- AngularJS API之$injector ---- 依赖注入
- AngularJS API之extend扩展对象
- AngularJS API之equal比较对象
- Elasticsearch之_default_—— 为索引添加默认映射
- Elasticsearch 动态映射——自动检测
- Elaticsearch REST API常用技巧
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- 动态加载 ExtJS 类库
- Visual Studio 2013 下 NuGet 无法识别自定义包路径的解决方法
- Xcode 5 中 xib 文件格式的调整
- 调用新浪微博显示用户信息
- 如何用代码动态生成ABAP类型
- 使用自签名证书内部分发 iOS7 应用
- 在 MvvmCross 下使用 iOS Storyboard
- 动态隐藏某些特殊类型的SAP CRM附件
- 设计模式之生成器模式
- Git 客户端保存用户名和密码
- SAP WebClient UI component模型元数据解析工具
- 设计模式之抽象工厂模式
- 测试面试题集锦(四)| Linux 与 Python 编程篇(附答案)
- 设计模式之代理模式
- SQL Server 中的 ROW_NUMBER 函数