我正在使用jwt-auth库,它使用类型提示注入AuthManager:
use Illuminate\Auth\AuthManager;
class Basic extends Authorization
{
public function __construct(AuthManager $auth, $identifier = 'email')
{
$this->auth = $auth;
$this->identifier = $identifier;
}
问题是,如果我使用中间件jwt.auth:
app('Dingo\Api\Routing\Router')->version('v1', ['middleware' => ['jwt.auth'] , 'prefix' => 'api', 'providers' => ['jwt']], function ($api) {
$api->get('protected', function () {
$token = JWTAuth::getToken();
return $token;
});
});
我收到此错误:
{"message":"Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\\Auth\\AuthManager","status_code":500,"debug":{"line":839,"file":"\/share\/vendor\/illuminate\/container\/Container.php","class":"Illuminate\\Contracts\\Container\\BindingResolutionException"
那么,问题是,如何正确注入AuthManager?为什么$ app没有解决?
答案 0 :(得分:3)
尝试在AuthManager
文件中注入bootstrap/app.php
:
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
// Injecting goes here
$app->singleton(Illuminate\Auth\AuthManager::class, function ($app) {
return $app->make('auth');
});
我们知道如果我们运行Illuminate\Auth\AuthManager
,Illuminate\Auth\AuthServiceProvider
将自动解决。参见:
Illuminate\Auth\AuthServiceProvider@registerAuthenticator
因此,我们必须在使用AuthManager
之前运行此服务提供程序。但是流明略有不同。我看到Illuminate\Auth\AuthManager
尚未注册:
Laravel\Lumen\Application::$availableBindings
当容器想要解析资源时,使Lumen运行得更快是一个黑客攻击,请参阅:
Laravel\Lumen\Application@make
所以,基本上,如果你想解决Illuminate\Auth\AuthManager
类及其依赖关系,你可以先使用它来注册它的类绑定。
我们知道
Laravel\Lumen\Application::$availableBindings
属性在public
可见性中,因此也适用:
/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
$app->availableBindings['Illuminate\Auth\AuthManager'] = 'registerAuthBindings';
$app->alias('auth', 'Illuminate\Auth\AuthManager');
据我所知,如果我们想要使用this library在Lumen中实现JWT身份验证,那么会遇到很多问题。所以,我做了一个与这个库很好地集成的自举(干净启动)流明应用程序。请查看my repo。我将添加有关哪一个以及为什么我们稍后应该更改代码的说明。欢呼声。
答案 1 :(得分:0)
在注册现有的Laravel LIKE
时,我使用了SessionManager
并使用了同样无法解析的$app
变量。
在阅读@krisanalfa的答案后,我试图窥视SessionServiceProvider
中的$availableBindings
值,它看起来像这样:
Application.php
每个键的值表示将用于加载实现的方法,这些方法也位于public $availableBindings = [
'auth' => 'registerAuthBindings',
'auth.driver' => 'registerAuthBindings',
'Illuminate\Auth\AuthManager' => 'registerAuthBindings',
'Illuminate\Contracts\Cache\Factory' => 'registerCacheBindings',
'Illuminate\Contracts\Cache\Repository' => 'registerCacheBindings',
....
];
内部。
如果您需要加载配置并注册绑定:
Application.php
但是,如果该服务不需要任何配置,则只需注册即可:
protected function registerAuthBindings()
{
$this->singleton('auth', function () {
return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth');
});
$this->singleton('auth.driver', function () {
return $this->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth.driver');
});
...
}
撰写本文时的来源:https://github.com/laravel/lumen-framework/blob/5.8/src/Application.php
希望这对以后的人有帮助。这花了我很多时间。