为什么不会调用Policy能力方法?

时间:2017-01-25 11:20:27

标签: laravel laravel-5.2

我使用生成器生成一个简单的策略:

php artisan make:policy TeamPolicy

而且,我已在AuthServiceProvider注册为:

 protected $policies = [
        Team::class => TeamPolicy::class,
    ];

我试图在TeamsController中将其称为:

$this->authorize('update', $team);

以下是我的政策文件Policies\TeamPolicy.php

<?php
namespace App\Policies;
use App\Team;
use Illuminate\Auth\Access\HandlesAuthorization;
class TeamPolicy
{
    use HandlesAuthorization;
    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    public function update( Team $team)
    {
        App:debug("Policy update called!");
        return true;
    }
}

但是,从不调用策略中的更新方法,并且在调用$this->authorize('update', $team);

时收到错误403

请指教!

1 个答案:

答案 0 :(得分:2)

Policy方法的第一个参数应该是用户检查授权。请尝试改为:

public function update(User $user, Team $team)
{
    //...
}