Skip to main content

Using Gates Properly: Remove isAble() Method

Premium
3 min read

The next thing I don't like is a specific ->isAble() method instead of using Laravel default Gates/Policies:

app/Http/Controllers/Api/V1/OrderController.php

public function show(Order $order)
{
$this->isAble('view', $order); // policy
 
// ...
}
 
public function update(UpdateOrderRequest $request, Order $order)
{
try {
$this->isAble('update', $order);
 
// ...

This method comes from the base ApiController and looks like this:

app/Http/Controllers/Api/V1/ApiController.php

class ApiController extends Controller
{
protected $policyClass;
 
public function isAble($ability, $model)
{
return Gate::authorize($ability, [$model, $this->policyClass]);
}

I've been honestly trying to understand the purpose of this extra layer. Maybe I'm wrong here, but I don't see its benefit over just calling Gate::authorize() directly from Controllers.

So, this is my refactored "Laravel way" version...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.