Laravel: Auth guard [sanctum] 未定义
Laravel: Auth guard [sanctum] is not defined
我正在尝试通过 Sanctum. I did all steps from documentation. Generation of Token 在 Laravel 中实现身份验证工作正常但是当我尝试使用 auth:sanctum
中间件时它 returns 错误 Auth guard [sanctum] is not defined.
这是我的文件:
/routes/api.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Dashboard;
Route::middleware('auth:sanctum')->get('/dashboard/get_current_client/', [Dashboard::class, 'get_current_client']);
Route::get('/dashboard/client_data/', [Dashboard::class, 'client_data']);
/config/auth.php
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
/app/Models/Client.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Client extends Authenticatable
{
use HasFactory, HasApiTokens, Notifiable;
protected $table = 'client';
protected $primaryKey = 'client_id';
public $timestamps = false;
}
/app/Http/Controllers/Dashboard.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Client;
class Dashboard extends Controller
{
public function client_data () {
$user = Client::where('client_id', 1)->first();
return $user->client_id;
}
public function get_current_client(Request $request) {
var_dump($request->user());
}
}
我发现了问题。由于某种原因,/bootstrap/cache 中的缓存尚未更新。手动删除后,一切正常。
我正在尝试通过 Sanctum. I did all steps from documentation. Generation of Token 在 Laravel 中实现身份验证工作正常但是当我尝试使用 auth:sanctum
中间件时它 returns 错误 Auth guard [sanctum] is not defined.
这是我的文件:
/routes/api.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Dashboard;
Route::middleware('auth:sanctum')->get('/dashboard/get_current_client/', [Dashboard::class, 'get_current_client']);
Route::get('/dashboard/client_data/', [Dashboard::class, 'client_data']);
/config/auth.php
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
/app/Models/Client.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Client extends Authenticatable
{
use HasFactory, HasApiTokens, Notifiable;
protected $table = 'client';
protected $primaryKey = 'client_id';
public $timestamps = false;
}
/app/Http/Controllers/Dashboard.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Client;
class Dashboard extends Controller
{
public function client_data () {
$user = Client::where('client_id', 1)->first();
return $user->client_id;
}
public function get_current_client(Request $request) {
var_dump($request->user());
}
}
我发现了问题。由于某种原因,/bootstrap/cache 中的缓存尚未更新。手动删除后,一切正常。