(feat) Routing: route parameter constraints - #349
Open
techmahedy wants to merge 2 commits into
Open
techmahedy wants to merge 2 commits into
techmahedy wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Pull Request Checklist
{id:regex}was parsed but ignored)Summary
A route parameter used to match any single URI segment, so
posts/{id}also matched/posts/abcand the handler received a value in the wrong format. There was no way to constrain it. This PR adds parameter constraints to both attribute-based and file-based routes.A request that fails the constraint skips that route: the router tries the next one, then falls back to the existing 404. This also allows
posts/{id}(numeric) andposts/{slug}to coexist.Bug fix
The router already parsed
{param:regex}(inconvertRouteToRegex,extractRouteParameters,route()andgetCurrentRouteMiddleware) but then discarded the regex and always matched[^\/]+. The inline form is now honoured.New API
->where(string|array $name, ?string $expression)->whereNumber(),whereAlpha(),whereAlphaNumeric(),whereUuid(),whereUlid()->whereIn($param, array $values)Route::pattern($name, $expression){id:regex}in a URI#[Route(..., where: [...])]Compatibility
#[Route]gainedwhereas its last constructor argument, so positional callers are unaffected.Files
src/Phaseolies/Support/Router.php:where*(),pattern(), constraint-aware regex compilation, route re-keying, validation.src/Phaseolies/Support/Router/Attributes/Route.php:whereargument.src/Phaseolies/Support/Facades/Route.php: docblock for the new methods.tests/Router/RouteConstraintsTest.php: new.Checklist