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.
I find model policies methods to be redundant. Often, model policies look something like:
This PR introduces a
fallback
method which allows policies to be streamlined. Usingfallback
, the model policy above could look something like:Much like
before
,fallback
is only called if prior methods in the chain do not exist or returnnull
. It also receives the authenticated user, name of the original ability, model (optional), and any additional arguments (optional).While this is technically a breaking change, it would only affect model policies which define a
fallback
method. I'd consider that a very low impact compared to what is gained. Nonetheless, I am targeting Laravel 12.Why not something else?
I went through a lot of combinations in my livestream, In the end, a singular method (i.e. not
userFallback
,guestFallback
,withoutModelFallback
, etc) provided the most flexibility when combined with the existing methods while having the least impact.As far as the name, its behavior is much more akin to
Route::fallback
than anafter
callback in the sense that it is only called if prior methods returnnull
and not simplyafter
them. It can also affect the outcome.