Laravel 5.3 用户验证源码探究 (一) 路由与注册

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’, ‘HomeController@index’);

Auth::routes() 是登陆、注册需要的一些路由;下面是定义一个 /home 路由,交给 HomeController@index 处理。

那么,就从路由开始我们的探究之旅吧。

路由

我们首先看看 Auth::routes(),定义在 vendor/laravel/framework/src/Illuminate/Support/Facades/Auth.php

  public static function routes()
    {
        static::$app->make('router')->auth();
    }

这里由 IoC 容器 解析了一个 Illuminate\Routing\Router 类的实例,再调用里面的 auth() 方法。

我们再来看看 auth() 方法,定义在 vendor/laravel/framework/src/Illuminate/Routing/Router.php

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

        // Registration Routes...
        $this->get('register', 'Auth\RegisterController@showRegistrationForm');
        $this->post('register', 'Auth\RegisterController@register');

        // Password Reset Routes...
        $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
        $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
        $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
        $this->post('password/reset', 'Auth\ResetPasswordController@reset');
    }

这里定义了 登陆注销注册密码重置 的路由。

先看看注册部分。

注册

App\Http\Controllers\Auth\RegisterController 负责注册的逻辑,这里 use 了 Illuminate\Foundation\Auth\RegistersUsers 这个 trait ,包含注册时通用的一些逻辑。

路由 get('/register') 所绑定的方法 Auth\RegisterController@showRegistrationForm 就定义在这个 trait 里:

  public function showRegistrationForm()
    {
        return view('auth.register');
    }

很简单,返回一个 auth.register 视图。

auth.register 视图获取用户的输入: nameemailpassword,然后 POST 提交到 ‘/register’。

再来看看路由 post('/register') 所绑定的方法 Auth\RegisterController@register

同样, register 方法定义在 Illuminate\Foundation\Auth\RegistersUsers 里:

 public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        $this->guard()->login($this->create($request->all()));

        return redirect($this->redirectPath());
    }

首先使用请求传入的表单调用 validator() ,返回一个验证对象,再调用 validate() 验证表单内容的合法性。

validator() 定义在 App\Http\Controllers\Auth\RegisterController 里:

 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() 这里会调用 Illuminate\Foundation\Auth\RegistersUsers 里的 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' => App\User::class,
        ],
    ],

 

默认的 guardwebweb 这个 guard 采用 session 驱动, 数据提供者是 usersusers 数据提供者使用 eloquent 驱动, 使用 App\User::class 模型。

具体这个 guard 是怎么生成的,这里暂时先不探究,放到登陆验证里再详细说明。

接下来调用 guardlogin($this->create($request->all()))

首先是 $this->create() ,这个方法定义在 App\Http\Controllers\Auth\RegisterController 里:

 protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

使用 User 模型对输入的内容新增一条记录,并返回这个模型的对象。

同样,如果需要修改注册时使用的字段,也是改写这个方法。

生成的 User 对象交给 guardlogin() 方法,做一系列登录的操作,具体怎么做的,还是放到登陆验证里再详细说明。

最后, return redirect($this->redirectPath()); 完成了注册、登陆的操作,最后跳转到我们在 App\Http\Controllers\Auth\RegisterController 里设置的 protected $redirectTo = '/home'; 目标 URI。

可以看一下 $this->redirectPath() 方法怎么写的,在 Illuminate\Foundation\Auth\RedirectsUsers 这个 trait 里:

 public function redirectPath()
    {
        return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
    }

如果定义了 $redirectTo 这个属性,就按照这个属性返回;如果没有,返回 ‘/home’。

这里把这个方法写成 trait 是因为这个方法还会在 App\Http\Controllers\Auth\LoginController 登陆控制器里使用,所以就把 redirectPath() 这个方法提出来做成一个 trait ,严格遵守 DRY 原则。

到这里,就完成了注册的所有过程。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/113084.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 退出多个activity的方法

    退出多个activity的方法

    2022年2月21日
    30
  • mac idea激活码永久【中文破解版】2022.02.13

    (mac idea激活码永久)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~4KDDGND3CI-eyJsaWNlbnNlSWQiOi…

    2022年4月1日
    50
  • Mina框架的使用[通俗易懂]

    什么是Mina框架ApacheMina是一个能够帮助用户开发高性能和高伸缩性网络应用程序的框架。它通过Javanio技术基于TCP/IP和UDP/IP协议提供了抽象的、事件驱动的、异步的API。是用来代替NIO网络框架的,对NIO框架进行了一层封装的Socket库。Mina主页下载地址为什么使用Mina?传统socket:阻塞式通信每建立一个Socket连接时,同时创建一个新线程对该Soc

    2022年4月17日
    91
  • PHP进程间通信-信号

    PHP进程间通信-信号

    2022年2月11日
    39
  • HTML5播放暂停音乐

    查看效果:http://hovertree.com/code/jquery/ueyf7gn4.htm代码如下:更多特效:http://www.cnblogs.com/roucheng/p/texi

    2021年12月22日
    44
  • 第二篇 FastAI数据准备「建议收藏」

    第二篇 FastAI数据准备「建议收藏」一、FastAI代码组织结构(文档链接)FastAI库主要涉及神经网络在如下四个领域的应用:collab(协同滤波问题)、tabular(结构化数据或者说表格数据处理)、text(自然语言处理)、vision(机器视觉)。对每一领域(除了collab),其下又会按照如下结构组织代码:(1)data:定义了模型所需的数据集类。(2)transform:数据预处理(如对图像数据的图像…

    2022年9月8日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号