Laravel 5.3 用户验证源码探究 路由与注册
Posted 码农编程进阶笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel 5.3 用户验证源码探究 路由与注册相关的知识,希望对你有一定的参考价值。
https://blog.csdn.net/realghost/article/details/52558962
简介
Laravel 从 5.2 开始就有了开箱即用的用户验证,5.3 又在 5.2 的基础上又有了一些改变。为了深入了解具体的用户验证实现,只能深入 Laravel 的源码,探究用户验证是怎么处理的。
开始
安装好 Laravel 5.3 的框架后,执行下面的命令
php artisan make:auth
该命令会在项目里添加以下文件(目录)
app/Http/Controller/HomeController.php resources/views/auth/ resources/views/auth/login.blade.php resources/views/auth/passwords/ resources/views/auth/passwords/email.blade.php resources/views/auth/passwords/reset.blade.php resources/views/auth/register.blade.php resources/views/home.blade.php resources/views/layouts/ resources/views/layouts/app.blade.php
除了一个 HomeController
是处理用户登陆之后的逻辑,其他都是一些视图,用于显示相应的页面。
在 routes/web.php
里添加了以下内容
Auth::routes();
Route::get(‘/home’, ‘[email protected]’);
Auth::routes()
是登陆、注册需要的一些路由;下面是定义一个 /home
路由,交给 [email protected]
处理。
那么,就从路由开始我们的探究之旅吧。
路由
我们首先看看 Auth::routes()
,定义在 vendor/laravel/framework/src/Illuminate/Support/Facades/Auth.php
:
public static function routes() { static::$app->make(‘router‘)->auth(); }
这里由 IoC 容器 解析了一个 IlluminateRoutingRouter 类的实例,再调用里面的 auth()
方法。
我们再来看看 auth() 方法,定义在 vendor/laravel/framework/src/Illuminate/Routing/Router.php
:
public function auth() { // Authentication Routes... $this->get(‘login‘, ‘Auth[email protected]‘)->name(‘login‘); $this->post(‘login‘, ‘Auth[email protected]‘); $this->post(‘logout‘, ‘Auth[email protected]‘); // Registration Routes... $this->get(‘register‘, ‘Auth[email protected]‘); $this->post(‘register‘, ‘Auth[email protected]‘); // Password Reset Routes... $this->get(‘password/reset‘, ‘Auth[email protected]‘); $this->post(‘password/email‘, ‘Auth[email protected]‘); $this->get(‘password/reset/{token}‘, ‘Auth[email protected]‘); $this->post(‘password/reset‘, ‘Auth[email protected]‘); }
这里定义了 登陆 、 注销 、注册 和 密码重置 的路由。
先看看注册部分。
注册
AppHttpControllersAuthRegisterController
负责注册的逻辑,这里 use 了 IlluminateFoundationAuthRegistersUsers
这个 trait ,包含注册时通用的一些逻辑。
路由 get(‘/register‘)
所绑定的方法 Auth[email protected]ionForm
就定义在这个 trait 里:
public function showRegistrationForm() { return view(‘auth.register‘); }
很简单,返回一个 auth.register
视图。
auth.register
视图获取用户的输入: name
,email
,password
,然后 POST 提交到 ‘/register’。
再来看看路由 post(‘/register‘)
所绑定的方法 Auth[email protected]
。
同样, register
方法定义在 IlluminateFoundationAuthRegistersUsers
里:
public function register(Request $request) { $this->validator($request->all())->validate(); $this->guard()->login($this->create($request->all())); return redirect($this->redirectPath()); }
首先使用请求传入的表单调用 validator()
,返回一个验证对象,再调用 validate()
验证表单内容的合法性。
validator()
定义在 AppHttpControllersAuthRegisterController
里:
protected function validator(array $data) { return Validator::make($data, [ ‘name‘ => ‘required|max:255‘, ‘email‘ => ‘required|email|max:255|unique:users‘, ‘password‘ => ‘required|min:6|confirmed‘, ]); }
在这里给出了输入表单的验证规则,如果我们的用户注册需要的表单与这几个字段不一致(例如需要添加一个手机号),就在这里修改。
返回的 Validator
对象会在 register()
方法里验证。
再回到 register()
方法, 往下走 $this->guard()->login($this->create($request->all()));
。
$this->guard()
这里会调用 IlluminateFoundationAuthRegistersUsers
里的 guard()
:
protected function guard() { return Auth::guard(); }
这里无参数调用 Auth::guard()
返回一个默认的 guard ,看一下 config/auth.php
:
‘defaults‘ => [ ‘guard‘ => ‘web‘, ‘passwords‘ => ‘users‘, ], ‘guards‘ => [ ‘web‘ => [ ‘driver‘ => ‘session‘, ‘provider‘ => ‘users‘, ], ‘api‘ => [ ‘driver‘ => ‘token‘, ‘provider‘ => ‘users‘, ], ], ‘providers‘ => [ ‘users‘ => [ ‘driver‘ => ‘eloquent‘, ‘model‘ => AppUser::class, ], ],
默认的 guard 是 web ; web 这个 guard 采用 session 驱动, 数据提供者是 users ;users 数据提供者使用 eloquent 驱动, 使用 AppUser::class
模型。
具体这个 guard 是怎么生成的,这里暂时先不探究,放到登陆验证里再详细说明。
接下来调用 guard 的 login($this->create($request->all()))
。
首先是 $this->create()
,这个方法定义在 AppHttpControllersAuthRegisterController
里:
protected function create(array $data) { return User::create([ ‘name‘ => $data[‘name‘], ‘email‘ => $data[‘email‘], ‘password‘ => bcrypt($data[‘password‘]), ]); }
使用 User
模型对输入的内容新增一条记录,并返回这个模型的对象。
同样,如果需要修改注册时使用的字段,也是改写这个方法。
生成的 User
对象交给 guard 的 login()
方法,做一系列登录的操作,具体怎么做的,还是放到登陆验证里再详细说明。
最后, return redirect($this->redirectPath());
完成了注册、登陆的操作,最后跳转到我们在 AppHttpControllersAuthRegisterController
里设置的 protected $redirectTo = ‘/home‘;
目标 URI。
可以看一下 $this->redirectPath()
方法怎么写的,在 IlluminateFoundationAuthRedirectsUsers
这个 trait 里:
public function redirectPath() { return property_exists($this, ‘redirectTo‘) ? $this->redirectTo : ‘/home‘; }
如果定义了 $redirectTo 这个属性,就按照这个属性返回;如果没有,返回 ‘/home’。
这里把这个方法写成 trait 是因为这个方法还会在
AppHttpControllersAuthLoginController
登陆控制器里使用,所以就把redirectPath()
这个方法提出来做成一个 trait ,严格遵守 DRY 原则。
到这里,就完成了注册的所有过程。
以上是关于Laravel 5.3 用户验证源码探究 路由与注册的主要内容,如果未能解决你的问题,请参考以下文章
任何想法如何使用 vue js 和 vue 路由器在 laravel 5.3 中进行基于会话的身份验证
没有身份验证的 Laravel 5.3 RESTFul API