在使用的时候,对一个中间件找不到位置.头疼不已,经过一顿寻找终于搞明白了,这里记录下,因为虽然注释了,不过几天后又蒙圈了.前后台一起搞真是玩自己啊.
通过dingo和jwt使用有一点小启示.就是服务提供者会在boot()里注册一些中间件或者扩展auth的guards驱动.
看代码吧,说也说不清,估计过几天又会看不懂的.
app.php
1 2 3 4
| $app->register(Dingo\Api\Provider\LumenServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
|
dingo/api
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| Dingo\Api\Provider;
class LumenServiceProvider extends DingoServiceProvider {
public function boot() { parent::boot();
$this->app->configure('api');
.... $this->app->routeMiddleware([ 'api.auth' => Auth::class, 'api.throttle' => RateLimit::class, 'api.controllers' => PrepareController::class, ]); }
|
jwt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class LumenServiceProvider extends AbstractServiceProvider{ .... }
protected function extendAuthGuard() { $this->app['auth']->extend('jwt', function ($app, $name, array $config) { $guard = new JwtGuard( $app['tymon.jwt'], $app['auth']->createUserProvider($config['provider']), $app['request'] );
$app->refresh('request', $guard, 'setRequest');
return $guard; }); }
|
config/auth
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php
return [
'defaults' => [ 'guard' => env('AUTH_GUARD', 'api'), ],
'guards' => [ 'api' => [ 'driver' => 'jwt', 'provider' => 'users', ], ],
|