1
votes

When I've implemented this (https://mattstauffer.co/blog/acl-access-control-list-authorization-in-laravel-5-1) authorization laravel 5.1 tutorial, I've found the usage of this

if (Gate::allows('create-contact')) {
    redirect('hooray');
}

And then the framework shows me this error

Call to undefined method Illuminate\Auth\Access\Gate::allow()

Everything is OK with the dependencies, so that's not the problem, then I found that laravel started to implement this method since version 5.1.11 in this topic at laravel's docs for authorization (laraveldocs/5.1/authorization) So laravel team is saying in their documentation page that this authorization feature is available since version 5.1.11.

I was getting the same error, so I went to the laravel/framework repository on github (github/laravel/framework/) and check from version 5.1.11 to 5.1.31 and found that method doesn't exist in the source code.

That gate method is available since version 5.2

You can check both of them here for v5.1 (github/laravel/framework/blob/v5.1.31/src/Illuminate/Contracts/Auth/Access/Gate.php) and for v5.2 (github/laravel/framework/blob/v5.2.0/src/Illuminate/Contracts/Auth/Access/Gate.php)

So I wanted to share this knowledge with you, but if you find a solution, without upgrading the version of the framework, I would be glad to know.

Thanks!

1
This is just an idea (I'm not saying you should do it) but if you really can't upgrade the whole framework to 5.2, could you just pull in the 5.2 illuminate package(s) you need, namely Illuminate\Auth packagist.org/packages/illuminate/auth#v5.2.0 ? - haakym

1 Answers

0
votes

I think that's just the result of a typo in your code. The method is called allows, but your error reports allow (as in the singular of allows):

Call to undefined method Illuminate\Auth\Access\Gate::allow()
//                            This should be "allows" ^^^^^

The Illuminate\Contracts\Auth\Access\Gate interface you're referencing is just a contract that the Gate class must implement, just because the allows method is not required by it that doesn't mean it is not implemented. It is in fact implemented by Illuminate\Auth\Access\Gate, which is the actual implementation that is bound to that contract.