'Illegal offset type' 尝试在 Lumen 上创建我自己的身份验证
'Illegal offset type' while trying to create my own Authentication on Lumen
我正在尝试根据外部 Oauth2 提供商为我的 API 创建我自己的身份验证。我使用 Lumen 作为我的 API 后端,我必须为没有有效访问令牌的用户保护我的一些端点。
所以我按照 docs 中写的开始。我在 bootstrap/app.php
中取消了 $app->register(App\Providers\AuthServiceProvider::class);
的注释
我在 AuthServiceProvider
中创建了特定的登录名:
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
// Here you may define how you wish users to be authenticated for your Lumen
// application. The callback which receives the incoming request instance
// should return either a User instance or null. You're free to obtain
// the User instance via an API token or any other method necessary.
$this->app['auth']->viaRequest('api', function ($request) {
// my logic
});
}
}
而且我还在 routes/web/php
:
中保护了我的路线
$router->group(['prefix' => 'api', 'middleware' => 'auth'], function () use ($router){
$router->get('grant-access', ['uses' => 'DatabaseController@grantAccess']);
$router->get('refresh-access', ['uses' => 'DatabaseController@refreshAccess']);
});
我调查了 AuthManager
中有一个方法 guard()
导致了问题:
/**
* Attempt to get the guard from the local cache.
*
* @param string|null $name
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
*/
public function guard($name = null)
{
$name = $name ?: $this->getDefaultDriver();
return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);
}
因为变量 $name
是一个对象,对象不能用作 PHP 数组中的键。
所以我的问题是我错过了在 Lumen 中打开身份验证的内容吗?
好的,我花了几分钟,但我发现我还必须在 bootstrap/app.php
:
中取消注释
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
我相信这也应该在 Lumen docs 那个红色注释旁边提到。
我正在尝试根据外部 Oauth2 提供商为我的 API 创建我自己的身份验证。我使用 Lumen 作为我的 API 后端,我必须为没有有效访问令牌的用户保护我的一些端点。
所以我按照 docs 中写的开始。我在 bootstrap/app.php
$app->register(App\Providers\AuthServiceProvider::class);
的注释
我在 AuthServiceProvider
中创建了特定的登录名:
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
// Here you may define how you wish users to be authenticated for your Lumen
// application. The callback which receives the incoming request instance
// should return either a User instance or null. You're free to obtain
// the User instance via an API token or any other method necessary.
$this->app['auth']->viaRequest('api', function ($request) {
// my logic
});
}
}
而且我还在 routes/web/php
:
$router->group(['prefix' => 'api', 'middleware' => 'auth'], function () use ($router){
$router->get('grant-access', ['uses' => 'DatabaseController@grantAccess']);
$router->get('refresh-access', ['uses' => 'DatabaseController@refreshAccess']);
});
我调查了 AuthManager
中有一个方法 guard()
导致了问题:
/**
* Attempt to get the guard from the local cache.
*
* @param string|null $name
* @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
*/
public function guard($name = null)
{
$name = $name ?: $this->getDefaultDriver();
return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);
}
因为变量 $name
是一个对象,对象不能用作 PHP 数组中的键。
所以我的问题是我错过了在 Lumen 中打开身份验证的内容吗?
好的,我花了几分钟,但我发现我还必须在 bootstrap/app.php
:
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
我相信这也应该在 Lumen docs 那个红色注释旁边提到。