正确注入 AuthManager?
Properly inject AuthManager?
我正在使用 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 没有解决?
尝试在您的 bootstrap/app.php
文件中注入 AuthManager
:
/*
|--------------------------------------------------------------------------
| 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
class 和它的依赖关系,你可以在使用它之前先注册它的 class 绑定。
更新
我们知道
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');
更新 2
我知道如果我们想用 this library. So, I make a bootstrapped (clean start) Lumen Application that integrated well with this library. Please check out my repo 在 Lumen 中实现 JWT 身份验证会有很多问题。我将添加关于哪一个以及为什么我们稍后应该更改代码的解释。干杯。
在注册现有 Laravel SessionServiceProvider
.
时,我使用 SessionManager
使用相同的无法解析的 $app
变量遇到了这种情况
阅读@krisanalfa 的回答后,我试图查看 $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');
});
...
}
但如果该服务不需要任何配置,您只需像这样注册它:
protected function registerEventBindings()
{
$this->singleton('events', function () {
$this->register('Illuminate\Events\EventServiceProvider');
return $this->make('events');
});
}
撰写本文时的来源:https://github.com/laravel/lumen-framework/blob/5.8/src/Application.php
希望这对以后的其他人有所帮助。这花了我很多时间。
我正在使用 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 没有解决?
尝试在您的 bootstrap/app.php
文件中注入 AuthManager
:
/*
|--------------------------------------------------------------------------
| 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
class 和它的依赖关系,你可以在使用它之前先注册它的 class 绑定。
更新
我们知道
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');
更新 2
我知道如果我们想用 this library. So, I make a bootstrapped (clean start) Lumen Application that integrated well with this library. Please check out my repo 在 Lumen 中实现 JWT 身份验证会有很多问题。我将添加关于哪一个以及为什么我们稍后应该更改代码的解释。干杯。
在注册现有 Laravel SessionServiceProvider
.
SessionManager
使用相同的无法解析的 $app
变量遇到了这种情况
阅读@krisanalfa 的回答后,我试图查看 $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');
});
...
}
但如果该服务不需要任何配置,您只需像这样注册它:
protected function registerEventBindings()
{
$this->singleton('events', function () {
$this->register('Illuminate\Events\EventServiceProvider');
return $this->make('events');
});
}
撰写本文时的来源:https://github.com/laravel/lumen-framework/blob/5.8/src/Application.php
希望这对以后的其他人有所帮助。这花了我很多时间。