使用 livewire 和 laravel 的基本授权
basic authorization with livewire and laravel
我的帖子策略中有一个方法,只有当前经过身份验证的用户才能删除他自己的帖子。我尝试将此方法放入我的删除方法中,但 returns 该方法不存在。我在控制器中包含了 authroizerequest,所以我很困惑为什么会收到此错误。
方法App\Http\Livewire\Posts::授权不存在。
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
public $postId = null;
public function deletePost($id)
{
$this->authorize('delete', $this->postId);
$post = Post::find($id);
Storage::delete('public/photos/', $post->image);
$post->delete();
session()->flash('flash.banner', 'Post Deleted Successfully');
}
政策:
namespace App\Policies;
use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
public function delete(User $user, Post $post){
return $user->id === $post->user_id;
}
您只是导入使用 AuthorizesRequests 特性,不包括此特性在您的 class 您需要在您的控制器中使用此特性
class ControllerName extends Controller {
use AuthorizesRequests;
}
通过这种方式,您可以在 class.
中使用 AuthorizesRequest traits 方法
我的帖子策略中有一个方法,只有当前经过身份验证的用户才能删除他自己的帖子。我尝试将此方法放入我的删除方法中,但 returns 该方法不存在。我在控制器中包含了 authroizerequest,所以我很困惑为什么会收到此错误。
方法App\Http\Livewire\Posts::授权不存在。
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
public $postId = null;
public function deletePost($id)
{
$this->authorize('delete', $this->postId);
$post = Post::find($id);
Storage::delete('public/photos/', $post->image);
$post->delete();
session()->flash('flash.banner', 'Post Deleted Successfully');
}
政策:
namespace App\Policies;
use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
public function delete(User $user, Post $post){
return $user->id === $post->user_id;
}
您只是导入使用 AuthorizesRequests 特性,不包括此特性在您的 class 您需要在您的控制器中使用此特性
class ControllerName extends Controller {
use AuthorizesRequests;
}
通过这种方式,您可以在 class.
中使用 AuthorizesRequest traits 方法