我无法让策略在我的Laravel项目中工作,我安装了一个新项目从头开始测试,我有这个控制器:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index()
{
$this->authorize('viewAny', auth()->user());
return response("Hello world");
}
}
这项政策:
<?php
namespace App\Policies;
use Illuminate\Auth\Access\Response;
use App\Models\User;
class UserPolicy
{
public function viewAny(User $user): bool
{
return true;
}
}
这是我的模型
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
我收到错误403:此操作未经授权.我希望有人能帮我解决我的问题.谢谢
我还try 修改AuthServiceProvider文件,但没有任何更改.