Laravel 8 returns 403 未经授权的访问
Laravel 8 returns 403 UNAUTHORIZED ACCESS
我正在使用 Laravel 8,并且我有一个名为 Profile
的资源控制器。在此控制器中,用户可以编辑他们的用户配置文件信息。
所以在 edit
方法中,我把这个:
public function edit(User $user)
{
$this->authorize('edit-user', $user);
return view('editprofile', compact('user'));
}
并且用户可以通过此按钮访问 editprofile
blade:
<a href="{{ route('profile.edit' , ['profile' => Auth::user()->id]) }}" class="btn">Edit Profile</a>
但是现在的问题是,returns这个:
403 此操作未经授权。
但是我已经登录了,我想访问我自己的 profile.edit
而不是其他用户!
因此,如果您知道我为什么会收到此错误,请告诉我,我非常感谢你们的任何想法或建议...
谢谢。
这也是我的路线:
Route::middleware('auth')->group(function() {
Route::resource('profile' , ProfileController::class);
});
UDPATE 1:
请更改以下内容
- 路由参数名称是“profile”
所以,控制器方法参数必须是相同的“$profile”。
public function edit (Request $request, User $profile) {
Gate::allows('edit-user', $profile);
// do the logic
}
我有同样的错误。我变了
public function authorize()
{
return false;
}
至
public function authorize()
{
return true;
}
在App\Http\Requests\StoreProductRequest
我正在使用 Laravel 8,并且我有一个名为 Profile
的资源控制器。在此控制器中,用户可以编辑他们的用户配置文件信息。
所以在 edit
方法中,我把这个:
public function edit(User $user)
{
$this->authorize('edit-user', $user);
return view('editprofile', compact('user'));
}
并且用户可以通过此按钮访问 editprofile
blade:
<a href="{{ route('profile.edit' , ['profile' => Auth::user()->id]) }}" class="btn">Edit Profile</a>
但是现在的问题是,returns这个:
403 此操作未经授权。
但是我已经登录了,我想访问我自己的 profile.edit
而不是其他用户!
因此,如果您知道我为什么会收到此错误,请告诉我,我非常感谢你们的任何想法或建议...
谢谢。
这也是我的路线:
Route::middleware('auth')->group(function() {
Route::resource('profile' , ProfileController::class);
});
UDPATE 1:
请更改以下内容
- 路由参数名称是“profile” 所以,控制器方法参数必须是相同的“$profile”。
public function edit (Request $request, User $profile) {
Gate::allows('edit-user', $profile);
// do the logic
}
我有同样的错误。我变了
public function authorize()
{
return false;
}
至
public function authorize()
{
return true;
}
在App\Http\Requests\StoreProductRequest