如何正确访问 laravel 中的范围?
How to properly access a scope in laravel?
我目前正在使用 Laravel Passport,我可以通过使用下面 returns json 的 localhost:8000/api/check
来验证当前是否保存了令牌:
{"id":"1c080ff73c6592b8e35630ae36f45f5042c04d9a9ed26a7fafc3793c606484b619ed8792be65a658","user_id":1,"client_id":5,"name":"Personal Access Tokens","scopes":["administrator"],...}
但是当我尝试使用 localhost:8000/api/admin
为管理员使用中间件范围时 returns 出现错误
Illuminate\Contracts\Container\BindingResolutionException: Target class [scope] does not exist. in file
这里是routes/api.php
Route::group(['middleware' => 'auth:api'], function(){
Route::get('check', 'TeamController@check');
Route::group(['middleware' => 'scope:administrator'], function() {
Route::get('admin', 'TeamController@index');
});
});
下面是TeamController.php
上对应的函数
public function check(Request $request) {
return auth()->user()->token();
}
public function index(Request $request) {
return auth()->user()->token();
}
有人知道我做错了什么吗?
您很可能没有注册 scope
中间件。
"Passport includes two middleware that may be used to verify that an incoming request is authenticated with a token that has been granted a given scope. To get started, add the following middleware to the $routeMiddleware
property of your app/Http/Kernel.php
file:"
'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,
我目前正在使用 Laravel Passport,我可以通过使用下面 returns json 的 localhost:8000/api/check
来验证当前是否保存了令牌:
{"id":"1c080ff73c6592b8e35630ae36f45f5042c04d9a9ed26a7fafc3793c606484b619ed8792be65a658","user_id":1,"client_id":5,"name":"Personal Access Tokens","scopes":["administrator"],...}
但是当我尝试使用 localhost:8000/api/admin
为管理员使用中间件范围时 returns 出现错误
Illuminate\Contracts\Container\BindingResolutionException: Target class [scope] does not exist. in file
这里是routes/api.php
Route::group(['middleware' => 'auth:api'], function(){
Route::get('check', 'TeamController@check');
Route::group(['middleware' => 'scope:administrator'], function() {
Route::get('admin', 'TeamController@index');
});
});
下面是TeamController.php
上对应的函数public function check(Request $request) {
return auth()->user()->token();
}
public function index(Request $request) {
return auth()->user()->token();
}
有人知道我做错了什么吗?
您很可能没有注册 scope
中间件。
"Passport includes two middleware that may be used to verify that an incoming request is authenticated with a token that has been granted a given scope. To get started, add the following middleware to the
$routeMiddleware
property of yourapp/Http/Kernel.php
file:"
'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,