Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently to register a group of authorization abilities, we can use the
resource()
method, which defines our standard CRUD options. This is fine in a lot of cases, but if you want to adjust those basic options, you now need to pass a clunky third parameter in theresource()
method to get the desired abilities.With this PR we now have the ability to auto-register any methods defined in a class as a Gate ability! It uses the convention of the method name being the same as the ability name.
Imagine we have a class of gates:
To register this in our
AuthServiceProvider
we can now use:and all 7 of our methods will be defined with the following ability names:
Some people will probably be thinking, why not just use actual Laravel Policies? I've talked previously (#19124) about why I prefer defined gates vs policies, but it boils down to the fact policies will auto resolve the authorization rule based on the first Model it receives, and this isn't always desirable. Defined gates work more similarly to Routes, and allow you to be explicit about which authorization rule is called.
The one thing I'm not really sure about with this PR is if there are performance concerns when dealing with Reflection. I wasn't seeing any issues in my testing, but if anyone has any knowledge on this, would love to hear it.