Skip to content

fix: generate valid sample URIs for custom route placeholders#10206

Open
jalexiscv wants to merge 1 commit into
codeigniter4:developfrom
jalexiscv:fix/bug-9804-custom-placeholder-filters
Open

fix: generate valid sample URIs for custom route placeholders#10206
jalexiscv wants to merge 1 commit into
codeigniter4:developfrom
jalexiscv:fix/bug-9804-custom-placeholder-filters

Conversation

@jalexiscv
Copy link
Copy Markdown

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)), the php spark routes command shows <unknown> for filters on those routes. This happens because SampleURIGenerator only 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 caused FilterFinder to throw PageNotFoundException when trying to route the sample URI, resulting in <unknown> filter display.

Solution

Added generateSample() method to SampleURIGenerator that creates valid sample strings from regex patterns:

  • Character classes [a-z] → uses the first valid character
  • Quantifiers {n}, +, *, ? → repeats accordingly
  • Dot . → uses a
  • Escaped characters \d → uses the escaped char

This handles common regex patterns like [A-Z]{3}[0-9]+, [a-f0-9]{8}, etc.

Changes

One file: system/Commands/Utilities/Routes/SampleURIGenerator.php

  • Replace ::unknown:: fallback with generateSample($regex)
  • Add generateSample() private method

Testing

  • Custom placeholder [A-Z]{3}[0-9]+ → generates AAA0
  • Built-in :num → continues using predefined 123
  • Built-in :any → continues using predefined 123/abc
  • Plain route → unchanged ✓

Ref: #9804
Closes #9804

@mergeable
Copy link
Copy Markdown

mergeable Bot commented May 18, 2026

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
on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

@jalexiscv jalexiscv force-pushed the fix/bug-9804-custom-placeholder-filters branch from 0946044 to 31f6444 Compare May 18, 2026 03:07
Copy link
Copy Markdown
Contributor

@neznaika0 neznaika0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@jalexiscv jalexiscv force-pushed the fix/bug-9804-custom-placeholder-filters branch from 31f6444 to fc75029 Compare May 18, 2026 03:53
@jalexiscv
Copy link
Copy Markdown
Author

Thanks for the review @neznaika0! I've updated the PR based on your feedback:

  1. Removed generateSample() regex parser — it was unreliable for arbitrary patterns as you pointed out.
  2. Added sample parameter to addPlaceholder() — users can now specify exact samples:
    $routes->addPlaceholder("code", "[A-Z]{3}[0-9]+", "ABC123");
  3. Fallback priority: user-provided sample → built-in sample → placeholder name

This is a much cleaner approach. Let me know if you have any other concerns.

@neznaika0
Copy link
Copy Markdown
Contributor

It looks better.There are some shortcomings, but let's wait for the answers from the participants.
Most likely, you will need to change the branch to 4.8, add tests, update the guide and examples.
It may be worth considering default fillers.

Copy link
Copy Markdown
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests and a changelog entry. This should also target 4.8.

@paulbalandan paulbalandan added tests needed Pull requests that need tests docs needed Pull requests needing documentation write-ups and/or revisions. wrong branch PRs sent to wrong branch labels May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs needed Pull requests needing documentation write-ups and/or revisions. tests needed Pull requests that need tests wrong branch PRs sent to wrong branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Route Filters Are Ignored When Using Custom Placeholders in Routing

3 participants