开发者社区> 问答> 正文

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

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

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

展开
收起
社区秘书 2019-12-10 13:06:11 794 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
动态、高效,蚂蚁动态卡片的内核逻辑 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载