Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ class RouteCollection implements RouteCollectionInterface
*/
private ?string $httpHost = null;

/**
* Whether create() has already loaded the array helper.
*/
private bool $arrayHelperLoaded = false;

/**
* Flag to limit or not the routes with {locale} placeholder to App::$supportedLocales
*/
Expand Down Expand Up @@ -1399,8 +1404,11 @@ protected function create(string $verb, string $from, $to, ?array $options = nul

// Replace our regex pattern placeholders with the actual thing
// so that the Router doesn't need to know about any of this.
foreach ($this->placeholders as $tag => $pattern) {
$routeKey = str_ireplace(':' . $tag, $pattern, $routeKey);
// Without a ':' no placeholder can match, so skip the loop.
if (str_contains($routeKey, ':')) {
foreach ($this->placeholders as $tag => $pattern) {
$routeKey = str_ireplace(':' . $tag, $pattern, $routeKey);
}
}

// If is redirect, No processing
Expand All @@ -1417,7 +1425,11 @@ protected function create(string $verb, string $from, $to, ?array $options = nul

$name = $options['as'] ?? $routeKey;

helper('array');
// Not used here; kept so apps relying on the helper being loaded keep working.
if (! $this->arrayHelperLoaded) {
helper('array');
$this->arrayHelperLoaded = true;
}

// Don't overwrite any existing 'froms' so that auto-discovered routes
// do not overwrite any app/Config/Routes settings. The app
Expand Down
44 changes: 44 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,50 @@ public function testAddReplacesCustomPlaceholders(): void
$this->assertSame($expects, $routes);
}

public function testAddWithoutPlaceholdersKeepsExactKey(): void
{
$routes = $this->getCollector();

$routes->add('users/profile-edit', 'controller');
$routes->add('products/([a-z]+)/(\d+)', 'controller');

$expects = [
'users/profile-edit' => '\controller',
'products/([a-z]+)/(\d+)' => '\controller',
];

$this->assertSame($expects, $routes->getRoutes());
}

public function testAddReplacesCustomNonCapturingPlaceholderMixedWithDefaults(): void
{
$routes = $this->getCollector();
$routes->addPlaceholder('slug', '(?:[a-z]+-)*[a-z]+');

$routes->add('blog/(:num)/(:slug)/(:segment)', 'controller');

$expects = [
'blog/([0-9]+)/((?:[a-z]+-)*[a-z]+)/([^/]+)' => '\controller',
];

$this->assertSame($expects, $routes->getRoutes());
}

public function testAddReplacesPlaceholdersCaseInsensitivelyWithoutParentheses(): void
{
$routes = $this->getCollector();

$routes->add('users/(:NUM)', 'controller');
$routes->add('posts/:segment', 'controller');

$expects = [
'users/([0-9]+)' => '\controller',
'posts/[^/]+' => '\controller',
];

$this->assertSame($expects, $routes->getRoutes());
}

public function testAddRecognizesCustomNamespaces(): void
{
$routes = $this->getCollector();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ Changes
*******

- **Config:** Added the ``md`` key for ``Config\Mimes::$mimes`` for Markdown files.
- **Router:** Reduced route registration overhead in RouteCollection::create() by skipping placeholder replacement for route keys without : and calling helper('array') only once per collection.

************
Deprecations
Expand Down
Loading