2
它有時需要檢查用戶授權的行動不帶參數,如創建/存儲方法:如何在Laravel 5.2中定義一個沒有參數的策略方法?
class PostPolicy
{
use HandlesAuthorization;
/**
* Determine if the user can create a new post.
*
* @param \App\User $user
* @return bool
*/
public function create(User $user)
{
if($user->is_admin)
{
return true;
}
return false;
}
}
,並使用PostController中授權方法:
class PostController extends Controller
{
/**
* Show the form for creating a new post.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$this->authorize('create');
//...
}
}
但似乎政策方法沒有參數不能對應於控制器上的授權方法,並且授權總是失敗。
它堅持不工作,直到我明確定義授權方法中的類(\ App \ Post :: class)..使用App \ Post將無法工作。 –