Skip to content

Commit 255fbe5

Browse files
committed
fix: bug in route registration
$routes and $routesOptions are not the same structure. $routesOptions don't have route names. If there are two routes having the same `from` but not the same name, the route and the option were not connected correctly.
1 parent 828230b commit 255fbe5

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class RouteCollection implements RouteCollectionInterface
110110
* verb => [
111111
* routeName => [
112112
* 'route' => [
113-
* routeKey => handler,
113+
* routeKey(or from) => handler,
114114
* ]
115115
* ]
116116
* ],
@@ -133,6 +133,14 @@ class RouteCollection implements RouteCollectionInterface
133133
* Array of routes options
134134
*
135135
* @var array
136+
*
137+
* [
138+
* verb => [
139+
* routeKey(or from) => [
140+
* key => value,
141+
* ]
142+
* ],
143+
* ]
136144
*/
137145
protected $routesOptions = [];
138146

@@ -1239,12 +1247,16 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
12391247

12401248
$name = $options['as'] ?? $from;
12411249

1250+
helper('array');
1251+
12421252
// Don't overwrite any existing 'froms' so that auto-discovered routes
12431253
// do not overwrite any app/Config/Routes settings. The app
12441254
// routes should always be the "source of truth".
12451255
// this works only because discovered routes are added just prior
12461256
// to attempting to route the request.
1247-
if (isset($this->routes[$verb][$name]) && ! $overwrite) {
1257+
$fromExists = (dot_array_search('*.route.' . $from, $this->routes[$verb] ?? []) === null)
1258+
? false : true;
1259+
if ((isset($this->routes[$verb][$name]) || $fromExists) && ! $overwrite) {
12481260
return;
12491261
}
12501262

tests/system/Router/RouteCollectionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,35 @@ static function () {},
11891189
$this->assertSame($options, ['as' => 'admin', 'foo' => 'baz']);
11901190
}
11911191

1192+
public function testRoutesOptionsWithSameFromTwoRoutes()
1193+
{
1194+
$routes = $this->getCollector();
1195+
1196+
// This is the first route for `administrator`.
1197+
$options1 = [
1198+
'as' => 'admin',
1199+
'foo' => 'options1',
1200+
];
1201+
$routes->get(
1202+
'administrator',
1203+
static function () {},
1204+
$options1
1205+
);
1206+
// The second route for `administrator` should be ignored.
1207+
$options2 = [
1208+
'foo' => 'options2',
1209+
];
1210+
$routes->get(
1211+
'administrator',
1212+
static function () {},
1213+
$options2
1214+
);
1215+
1216+
$options = $routes->getRoutesOptions('administrator');
1217+
1218+
$this->assertSame($options, $options1);
1219+
}
1220+
11921221
public function testRoutesOptionsForDifferentVerbs()
11931222
{
11941223
$routes = $this->getCollector();

0 commit comments

Comments
 (0)