控制器授权
// From this:
public function edit(Contact $contact) {
if (Gate::cannot('update-contact', $contact)) {
abort(403);
}
return view('contacts.edit', ['contact' => $contact]);
}
// To this:
public function edit(Contact $contact) {
$this->authorize('update-contact', $contact);
return view('contacts.edit', ['contact' => $contact]);
}
$this->authorizeForUser($user, 'update-contact', $contact);class ContactsController extends Controller
{
public function __construct()
{
// This call does everything you see in the methods below.
// If you put this here, you can remove all authorize()
// calls in the individual resource methods here.
$this->authorizeResource(Contact::class);
}
public function index()
{
$this->authorize('view', Contact::class);
}
public function create()
{
$this->authorize('create', Contact::class);
}
public function store(Request $request)
{
$this->authorize('create', Contact::class);
}
public function show(Contact $contact)
{
$this->authorize('view', $contact);
}
public function edit(Contact $contact)
{
$this->authorize('update', $contact);
}
public function update(Request $request, Contact $contact)
{
$this->authorize('update', $contact);
}
public function destroy(Contact $contact)
{
$this->authorize('delete', $contact);
}
}Last updated