我刚刚开始使用Laravel 5.4中的策略来处理我的授权。我一直在关注官方文档并创建了PostPolicy。
<?php
namespace App\Policies;
use App\User;
use App\Post;
use Illuminate\Auth\Access\HandlesAuthorization;
class PostPolicy
{
use HandlesAuthorization;
public function delete(User $user, Post $post)
{
return false;
//return $user->id === $post->user_id;
}
}
我的目标是停止使用策略删除帖子的功能。我目前仍然能够删除,无法找到实现这些策略规则的方法。
您需要授权操作。例如,你可以这样做:
if ($user->can('delete', $post)) {
或者在控制器中:
$this->authorize('delete', $post);
https://laravel.com/docs/5.5/authorization#authorizing-actions-using-policies