【问题标题】:basic authorization with livewire and laravel使用 livewire 和 laravel 进行基本授权
【发布时间】:2021-09-08 16:34:51
【问题描述】:

我的 postpolicy 中有一个方法,只有当前经过身份验证的用户才能删除他自己的帖子。我尝试将此方法放在我的删除方法中,但它返回该方法不存在。我在控制器中包含了 authroizerequest,所以我很困惑为什么会出现这个错误。

方法 App\Http\Livewire\Posts::authorize 不存在。

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;
    }

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    你只是在你的类中导入了 use AuthorizesRequests trait 不包括这个 trait 你需要在你的控制器中使用这个 trait

    class ControllerName extends Controller {
      use AuthorizesRequests;
    }
    

    通过这种方式,您可以在类中使用 AuthorizesRequest 特征方法。

    【讨论】:

      猜你喜欢
      • 2021-03-06
      • 2013-04-13
      • 2021-02-19
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多