Adding filter()
method to Klein to use for middleware such as Auth
#317
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.
In this PR, I am proposing a new
filter()
method to be added to Klein. This method is to be used for middleware that wouldn't normally add to the number of matched routes. This method utilizes theRoute
class's built in$count_match
param that wasn't being publicly exposed.Use case:
I currently have catch-all routes set up to enable Auth in my application at different levels of access. These catch-all routes conditionally add to the number of
$matched
based on where the user is trying to access. Because of this, I cannot add a 404 listener when a user tries to access a page that doesn't exist. Instead of throwing a 404, Klein sees that my catch-all Auth route has been matched, and therefore doesn't see the route as nonexistent. With the newfilter()
method, this would no longer be an issue.Example:
With the provided fix, navigating to
/resource/some-fake-url
will return a 404 error. Without the provided fix, a blank response will be sent, and a 404 error will not occur.If you'd like more info about this very common use case, please let me know. Thanks for your time!