[8.x] Add exception as parameter to the missing() callbacks #38289
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.
This PR change the logic for the
missing()
method handling any implicitly bound route models not found - changes are:ModelNotFoundException
exception to the callback registered with the routesmissing()
Note that the
missing()
method was created in #36035 and made available in Laravel v8.26.0.Motivation
The
missing()
method (documented here) offers a great way to handle any missing implicitly bound models, but there might be cases where one would like to only handle some missing models differently - and that is now possible i with this PR.Expanding on the original example from PR #36035:
The example above will only redirect the user if the
slug
provided for theLocation
model is not found. If an invalidphoto
is provided a normal ModelNotFound exception would be thrown and handled normally.A real world example could be when using teams and wanting a separate logic for dealing with an invalid team while still keeping the normal 404 for other parameters.
Alternatives
An alternative approach could be to allow a second parameter for the
missing()
method which specifies exactly what route parameter to apply the closure for:I personally think this syntax is better, but unfortunately an approach like this would require the method
getMissing()
to return an array of callables which would be a breaking change.Note that this PR does not stand in the way of implementing the alternative syntax above in a later version of Laravel.