流明中的“ viaRequest”参数如何工作?

时间:2019-12-09 15:12:14

标签: php laravel lumen

我重新运行了Laravel 6.x Lumen Install。 我试图了解身份验证的工作原理,并且已经完成了一些教程。

现在我在AuthServiceProvider.php中有以下代码:

<?php

namespace App\Providers;

use App\User;
use Firebase\JWT\JWT;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;


//new Code
class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
     public function boot()
     {
       // Here you may define how you wish users to be authenticated for your Lumen
       // application. The callback which receives the incoming request instance
       // should return either a User instance or null. You're free to obtain
       // the User instance via an API token or any other method necessary.

       $this->app['auth']->viaRequest('api', function ($request) {
         $key = 'pawifjopawiejfpoaiwejfpoji';
         $jwt = preg_replace('/^Bearer (.*)/', '$1', $request->header('Authorization'));
         $decoded = JWT::decode($jwt, $key, ['HS256']);

         return User::where('email', $decoded->email)->first();
       });
     }
}

尤其是这部分在我的脸上加了问号:

$this->app['auth']->viaRequest('api', function ($request)

首先,我不知道$ this到底代表什么。我知道$ this基本上是类本身的类/实例,但是如果是这样,它的属性“ app”是什么。对我而言,“ app”应代表整个app,即整个流明项目的一个实例。但这在我看来并没有多大意义,因为$ this仅表示serviceprovider类的实例,而该实例本身并不表示完整的应用程序。 那么,索引“ auth”究竟引用了什么? 它引用什么,可以在哪里查找其定义(我假设它是一个类)。 接下来,viaRequest()的参数如何工作? 默认情况下,第一个参数是“ api”,所以我想这是某种配置,但是我还有其他选择,在哪里可以找到呢? 到目前为止,我尚未在官方文档中找到任何东西:( 第二个参数是闭包,我很好,但是viaRequest()方法可以使用更多可选参数吗? 如果是这样,我不知道在哪里查找! :(

0 个答案:

没有答案