fix: generate valid sample URIs for custom route placeholders#10206
fix: generate valid sample URIs for custom route placeholders#10206jalexiscv wants to merge 1 commit into
Conversation
|
Hi there, jalexiscv! 👋 Thank you for sending this PR! We expect the following in all Pull Requests (PRs).
Important We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works. If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md Sincerely, the mergeable bot 🤖 |
0946044 to
31f6444
Compare
neznaika0
left a comment
There was a problem hiding this comment.
You have not added tests for different regex variants. The user can set any complex expression. I doubt that this will be solved by the selection method.
It is preferable to specify the exact placeholders when adding them. This will be a PR feature for v4.8
Fixes codeigniter4#9804 When using custom placeholders with 'php spark routes', filters were shown as '<unknown>' because SampleURIGenerator could not produce valid sample URIs for unknown regex patterns. Solution: - Extend addPlaceholder() with optional $sample parameter that lets users specify a representative value for the placeholder - Add getPlaceholderSamples() to RouteCollection - Update SampleURIGenerator to use user-provided samples first, then built-in samples, then the placeholder name as fallback - Remove generateSample() regex parser (unreliable for arbitrary patterns) Usage: $routes->addPlaceholder('code', '[A-Z]{3}[0-9]+', 'ABC123'); Ref: codeigniter4#9804
31f6444 to
fc75029
Compare
|
Thanks for the review @neznaika0! I've updated the PR based on your feedback:
This is a much cleaner approach. Let me know if you have any other concerns. |
|
It looks better.There are some shortcomings, but let's wait for the answers from the participants. |
paulbalandan
left a comment
There was a problem hiding this comment.
Please add some tests and a changelog entry. This should also target 4.8.
Description
Fixes #9804 - Route Filters are shown as
<unknown>when custom placeholders are used.Problem
When using
addPlaceholder()to define custom route placeholders (e.g.,(:code)), thephp spark routescommand shows<unknown>for filters on those routes. This happens becauseSampleURIGeneratoronly had predefined sample values for built-in placeholders (any,segment,num,alpha,alphanum,hash).When an unknown placeholder is encountered, it fell back to
::unknown::which does not match the actual regex pattern. This causedFilterFinderto throwPageNotFoundExceptionwhen trying to route the sample URI, resulting in<unknown>filter display.Solution
Added
generateSample()method toSampleURIGeneratorthat creates valid sample strings from regex patterns:[a-z]→ uses the first valid character{n},+,*,?→ repeats accordingly.→ usesa\d→ uses the escaped charThis handles common regex patterns like
[A-Z]{3}[0-9]+,[a-f0-9]{8}, etc.Changes
One file:
system/Commands/Utilities/Routes/SampleURIGenerator.php::unknown::fallback withgenerateSample($regex)generateSample()private methodTesting
[A-Z]{3}[0-9]+→ generatesAAA0✓:num→ continues using predefined123✓:any→ continues using predefined123/abc✓Ref: #9804
Closes #9804