尝试在您的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\AuthServiceProvider,Illuminate\Auth\AuthManager 将被自动解析。见:
Illuminate\Auth\AuthServiceProvider@registerAuthenticator
所以,我们必须先运行这个服务提供者,然后才能使用AuthManager。但流明略有不同。我看到Illuminate\Auth\AuthManager 还没有注册:
Laravel\Lumen\Application::$availableBindings
当容器要解析资源时,让 Lumen 运行得更快是一个 hack,参见:
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');
更新 2
我知道如果我们想在 Lumen 中使用 this library 实现 JWT 身份验证会有很多问题。因此,我制作了一个与该库很好地集成的引导式(干净启动)流明应用程序。请查看my repo。我将在稍后添加关于哪一个以及为什么我们应该更改代码的解释。干杯。