JWT 错误Unresolvable dependency resolving

调用代码
$token = JWTAuth::attempt($credentials)
会报错。

BindingResolutionException in Container.php line 850:
Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Cache\CacheManager
in Container.php line 850
at Container->resolveNonClass(object(ReflectionParameter)) in Container.php line 817
at Container->getDependencies(array(object(ReflectionParameter)), array()) in Container.php line 788
at Container->build('Illuminate\Cache\CacheManager', array()) in Container.php line 644
at Container->make('Illuminate\Cache\CacheManager', array()) in Application.php line 211
at Application->make('Illuminate\Cache\CacheManager') in Container.php line 864
at Container->resolveClass(object(ReflectionParameter)) in Container.php line 819
at Container->getDependencies(array(object(ReflectionParameter)), array()) in Container.php line 788
at Container->build('Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter', array()) in Container.php line 644
at Container->make('Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter', array()) in Application.php line 211
at Application->make('Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter') in JWTAuthServiceProvider.php line 273
at JWTAuthServiceProvider->getConfigInstance('Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter') in JWTAuthServiceProvider.php line 158
at JWTAuthServiceProvider->Tymon\JWTAuth\Providers\{closure}(object(Application)) in Container.php line 290
at Container->Illuminate\Container\{closure}(object(Application), array()) in Container.php line 746
at Container->build(object(Closure), array()) in Container.php line 644
at Container->make('tymon.jwt.provider.storage', array()) in Application.php line 211
at Application->make('tymon.jwt.provider.storage') in Container.php line 1203
at Container->offsetGet('tymon.jwt.provider.storage') in JWTAuthServiceProvider.php line 213
at JWTAuthServiceProvider->Tymon\JWTAuth\Providers\{closure}(object(Application)) in Container.php line 290
at Container->Illuminate\Container\{closure}(object(Application), array()) in Container.php line 746
at Container->build(object(Closure), array()) in Container.php line 644
at Container->make('tymon.jwt.blacklist', array()) in Application.php line 211
at Application->make('tymon.jwt.blacklist') in Container.php line 1203
at Container->offsetGet('tymon.jwt.blacklist') in JWTAuthServiceProvider.php line 181
at JWTAuthServiceProvider->Tymon\JWTAuth\Providers\{closure}(object(Application)) in Container.php line 290
at Container->Illuminate\Container\{closure}(object(Application), array()) in Container.php line 746
at Container->build(object(Closure), array()) in Container.php line 644
at Container->make('tymon.jwt.manager', array()) in Application.php line 211
at Application->make('tymon.jwt.manager') in Container.php line 1203
at Container->offsetGet('tymon.jwt.manager') in JWTAuthServiceProvider.php line 197
at JWTAuthServiceProvider->Tymon\JWTAuth\Providers\{closure}(object(Application)) in Container.php line 290
at Container->Illuminate\Container\{closure}(object(Application), array()) in Container.php line 746
at Container->build(object(Closure), array()) in Container.php line 644
at Container->make('tymon.jwt.auth', array()) in Application.php line 211
at Application->make('tymon.jwt.auth') in Container.php line 1203
at Container->offsetGet('tymon.jwt.auth') in Facade.php line 175
at Facade::resolveFacadeInstance('tymon.jwt.auth') in Facade.php line 144
at Facade::getFacadeRoot() in Facade.php line 231
at Facade::__callStatic('attempt', array(array('international_code' => '86', 'phone' => '18576627189', 'password' => '123456'))) in TokenController.php line 30
at TokenController->create(object(Request))
at call_user_func_array(array(object(TokenController), 'create'), array(object(Request))) in Container.php line 508
at Container->call(array(object(TokenController), 'create'), array()) in RoutesRequests.php line 610
at Application->callControllerCallable(array(object(TokenController), 'create'), array()) in RoutesRequests.php line 576
at Application->callLumenController(object(TokenController), 'create', array(true, array('uses' => 'App\Http\Controllers\TokenController@create'), array())) in RoutesRequests.php line 550
at Application->callControllerAction(array(true, array('uses' => 'App\Http\Controllers\TokenController@create'), array())) in RoutesRequests.php line 512
at Application->callActionOnArrayBasedRoute(array(true, array('uses' => 'App\Http\Controllers\TokenController@create'), array())) in RoutesRequests.php line 497
at Application->handleFoundRoute(array(true, array('uses' => 'App\Http\Controllers\TokenController@create'), array())) in RoutesRequests.php line 394
at Application->Laravel\Lumen\Concerns\{closure}() in RoutesRequests.php line 650
at Application->sendThroughPipeline(array(), object(Closure)) in RoutesRequests.php line 400
at Application->dispatch(null) in RoutesRequests.php line 341
at Application->run() in index.php line 28
at require('/Users/panxiaohe/Sites/api/public/index.php') in server.php line 106
阅读 5.1k
1 个回答

我的Lumen版本是5.5,报错的原因是cacheauth没有开启,在Lumen默认是关闭的。

打开bootstrap/app.php,在你的jwt配置下开启cacheauth,添加如下代码:

$app->alias('cache', 'Illuminate\Cache\CacheManager');
$app->singleton(Illuminate\Auth\AuthManager::class, function ($app) {
    return $app->make('auth');
});

这样就可以解决了,happy coding!!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题