zoukankan      html  css  js  c++  java
  • 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 容器 解析了一个 IlluminateRoutingRouter 类的实例,再调用里面的 auth() 方法。

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

     public function auth()
        {
            // Authentication Routes...
            $this->get('login', 'AuthLoginController@showLoginForm')->name('login');
            $this->post('login', 'AuthLoginController@login');
            $this->post('logout', 'AuthLoginController@logout');
    
            // Registration Routes...
            $this->get('register', 'AuthRegisterController@showRegistrationForm');
            $this->post('register', 'AuthRegisterController@register');
    
            // Password Reset Routes...
            $this->get('password/reset', 'AuthForgotPasswordController@showLinkRequestForm');
            $this->post('password/email', 'AuthForgotPasswordController@sendResetLinkEmail');
            $this->get('password/reset/{token}', 'AuthResetPasswordController@showResetForm');
            $this->post('password/reset', 'AuthResetPasswordController@reset');
        }

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

    先看看注册部分。

    注册

    AppHttpControllersAuthRegisterController 负责注册的逻辑,这里 use 了 IlluminateFoundationAuthRegistersUsers 这个 trait ,包含注册时通用的一些逻辑。

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

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

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

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

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

    同样, 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,
            ],
        ],
    

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

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

    接下来调用 guardlogin($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 对象交给 guardlogin() 方法,做一系列登录的操作,具体怎么做的,还是放到登陆验证里再详细说明。

    最后, 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 原则。

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

  • 相关阅读:
    LF 第三章 装饰器和迭代器相关
    Python 文件管理
    Python 强制类型转换
    安装模块
    LF 第三章
    pep8 Python编码规则
    Help on module pyclbr:
    Help on class timedelta in module datetime:
    Help on function meshgrid in module numpy.lib.function_base:
    Help on module matplotlib.cm in matplotlib:
  • 原文地址:https://www.cnblogs.com/lxwphp/p/9355468.html
Copyright © 2011-2022 走看看