Skip to content

Commit 20906ab

Browse files
committed
fix(Router): preserve $0 reference, document behavior, and add changelog entry
1 parent 86f068e commit 20906ab

4 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Release Date: Unreleased
1717
## Bugs Fixed
1818

1919
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
20+
- **Router:** Fixed a bug where `Router::replaceBackReferences()` failed to match and replace multi-digit back-references (such as `$10` or higher) when `Routing::$multipleSegmentsOneParam` was enabled.
2021
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
2122
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.
2223

‎docs/incoming/routing.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ $routes->get('product/(:any)', 'Catalog::productLookup/$1');
10221022
will match **product/123**, **product/123/456**, **product/123/456/789** and so on. And if the URI is **product/123/456**, `123/456` will be passed to the first parameter of the `Catalog::productLookup()` method.
10231023

10241024
!!! note "Note"
1025-
When this option is enabled, consecutive digits in back-references (such as `$10`) are treated as a single reference rather than `$1` followed by `0`. If a back-reference does not have a matching captured group, it resolves to an empty string (`''`).
1025+
When this option is enabled, consecutive digits in back-references (such as `$10`) are treated as a single reference rather than `$1` followed by `0`. The `$0` back-reference resolves to the entire matched path. If a back-reference does not have a matching captured group, it resolves to an empty string (`''`).
10261026

10271027
## Auto Routing (Improved)
10281028

‎system/Router/Router.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ protected function checkRoutes(string $uri): bool
565565
*/
566566
private function replaceBackReferences(string $input, array $matches): string
567567
{
568-
$pattern = '/\$([1-9][0-9]*)/u';
568+
$pattern = '/\$(0|[1-9][0-9]*)/u';
569569

570570
return preg_replace_callback(
571571
$pattern,

‎tests/system/Router/RouterTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,13 @@ public static function provideMultipleSegmentsParamTrueWithManyPlaceholders(): i
10691069
'product/foo',
10701070
[''],
10711071
];
1072+
1073+
yield 'reference $0 resolves to entire matched path' => [
1074+
$nineSegments,
1075+
'Catalog::lookup/$0/$1',
1076+
'a/b/c/d/e/f/g/h/i',
1077+
['a/b/c/d/e/f/g/h/i', 'a'],
1078+
];
10721079
}
10731080

10741081
public function testLogsRouteAttributeInstantiationFailureWithContext(): void

0 commit comments

Comments
 (0)