3
我使用policies作爲用戶授權。如何爲訪客用戶使用策略?laravel授權政策中的訪客用戶
這裏是我的代碼:
在控制器:
class PostController extends Controller
{
public function index(Post $post)
{
$this->authorize($post);
return $post->all();
}
}
政策:
class PostPolicy
{
// This function executes only for authenticated users.
// I want to use it for guest users too
public function index(User $user)
{
return $user->can('get-posts');
}
}
我不能在策略中檢查它,因爲如果用戶未經過身份驗證,此功能不會執行 – Ildar
然後使用Auth中間件保護路由。 –