[PHP] 浅谈 Laravel Authentication 的 auth:api

auth:api 在 Laravel 的 Routing , Middleware , API Authentication 主题中都有出现。

在 Routing 部分可以知道 auth:api 是中间件的名字,代表某个中间件实现,使用方式为 Route::middleware('auth:api')。

在 Middleware 部分可以知道 auth:api 冒号后面的是 中间件参数,多个参数就用逗号分隔,也就是说 'api' 是 auth 中间件的参数。auth 中间件在 app/Http/Kernel.php 的路由中间件里定义:protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class ]。

Middleware 中间件都是在 handle 方法里对请求进行验证的,handle 前面两个参数是 $request, Closure $next。中间件参数在 handle 默认的两个参数后面,

比如 user:creater,editer 就表示 user 中间件的 handle 方法的第三个参数值是 creater, 第四个参数值是 editer。

在 API Authentication 部分,config/auth.php 配置有多个 guards,分别有自己的 driver 和 providers。

另外 Auth Facade (AuthManager) 提供了 Auth::guard() 方法用于决定 Auth 的所使用的 guard。

有关 auth:api 与 Auth 的意义大致如此。

相关:[PHP] 浅谈 Laravel Authentication 的 guards 与 providers

Link:https://www.cnblogs.com/farwish/p/11800803.html

猜你喜欢

转载自www.cnblogs.com/farwish/p/11800803.html