Skip to content

Commit e329303

Browse files
committed
fix: routes keys must not be its names
1 parent f3b2f32 commit e329303

File tree

3 files changed

+111
-8
lines changed

3 files changed

+111
-8
lines changed

system/Commands/Utilities/Routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function run(array $params)
112112
$sampleUri = $uriGenerator->get($route['route']);
113113
$filters = $filterCollector->get($route['method'], $sampleUri);
114114

115-
$routeName = ($route['route'] === $route['name']) ? '»' : $route['name'];
115+
$routeName = ($route['name'] === null) ? '»' : $route['name'];
116116

117117
$tbody[] = [
118118
strtoupper($route['method']),

system/Router/DefinedRouteCollector.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(private readonly RouteCollectionInterface $routeColl
3333
public function collect(): Generator
3434
{
3535
$methods = Router::HTTP_METHODS;
36+
$namesCollector = [];
3637

3738
foreach ($methods as $method) {
3839
$routes = $this->routeCollection->getRoutes($method);
@@ -49,7 +50,13 @@ public function collect(): Generator
4950
$handler = $view ? '(View) ' . $view : '(Closure)';
5051
}
5152

52-
$routeName = $this->routeCollection->getRoutesOptions($route, $method)['as'] ?? $route;
53+
$routeName = $this->routeCollection->getRoutesOptions($route, $method)['as'] ?? null;
54+
55+
if (in_array($routeName, $namesCollector[$method] ?? [])) {
56+
$routeName = null;
57+
} else {
58+
$namesCollector[$method][] = $routeName;
59+
}
5360

5461
yield [
5562
'method' => $method,

tests/system/Router/DefinedRouteCollectorTest.php

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,37 +67,37 @@ public function testCollect(): void
6767
[
6868
'method' => 'GET',
6969
'route' => 'journals',
70-
'name' => 'journals',
70+
'name' => null,
7171
'handler' => '\App\Controllers\Blogs',
7272
],
7373
[
7474
'method' => 'GET',
7575
'route' => '100',
76-
'name' => '100',
76+
'name' => null,
7777
'handler' => '\App\Controllers\Home::index',
7878
],
7979
[
8080
'method' => 'GET',
8181
'route' => 'product/([0-9]+)',
82-
'name' => 'product/([0-9]+)',
82+
'name' => null,
8383
'handler' => '\App\Controllers\Catalog::productLookupByID/$1',
8484
],
8585
[
8686
'method' => 'GET',
8787
'route' => 'feed',
88-
'name' => 'feed',
88+
'name' => null,
8989
'handler' => '(Closure)',
9090
],
9191
[
9292
'method' => 'GET',
9393
'route' => '200',
94-
'name' => '200',
94+
'name' => null,
9595
'handler' => '(Closure)',
9696
],
9797
[
9898
'method' => 'GET',
9999
'route' => 'about',
100-
'name' => 'about',
100+
'name' => null,
101101
'handler' => '(View) pages/about',
102102
],
103103
];
@@ -144,4 +144,100 @@ public function testCollectSameFromWithDifferentVerb(): void
144144
];
145145
$this->assertSame($expected, $definedRoutes);
146146
}
147+
148+
/**
149+
* @see https://github.com/codeigniter4/CodeIgniter4/pull/9499
150+
*/
151+
public function testCollectSameNameWithDifferentFrom(): void
152+
{
153+
$routes = $this->createRouteCollection();
154+
$routes->get('/', 'HomeController::index');
155+
$routes->post('login', 'AuthController::login', ['as' => 'login']);
156+
$routes->get('logout', 'AuthController::logout', ['as' => 'logout']);
157+
158+
$collector = new DefinedRouteCollector($routes);
159+
160+
$definedRoutes = [];
161+
162+
foreach ($collector->collect() as $route) {
163+
$definedRoutes[] = $route;
164+
}
165+
166+
$expected = [
167+
[
168+
'method' => 'GET',
169+
'route' => '/',
170+
'name' => null,
171+
'handler' => '\\App\\Controllers\\HomeController::index',
172+
],
173+
[
174+
'method' => 'GET',
175+
'route' => 'logout',
176+
'name' => 'logout',
177+
'handler' => '\\App\\Controllers\\AuthController::logout',
178+
],
179+
[
180+
'method' => 'POST',
181+
'route' => 'login',
182+
'name' => 'login',
183+
'handler' => '\\App\\Controllers\\AuthController::login',
184+
],
185+
];
186+
$this->assertSame($expected, $definedRoutes);
187+
}
188+
189+
/**
190+
* @see https://github.com/codeigniter4/CodeIgniter4/pull/9499
191+
*/
192+
public function testCollectSameNames(): void
193+
{
194+
$routes = $this->createRouteCollection();
195+
$routes->get('/', 'HomeController::index');
196+
$routes->post('login', 'AuthController::login', ['as' => 'login']);
197+
$routes->post('user/login', 'UserController::login', ['as' => 'login']);
198+
$routes->get('logout', 'AuthController::logout', ['as' => 'logout']);
199+
$routes->get('user/logout', 'UserController::logout', ['as' => 'logout']);
200+
201+
$collector = new DefinedRouteCollector($routes);
202+
203+
$definedRoutes = [];
204+
205+
foreach ($collector->collect() as $route) {
206+
$definedRoutes[] = $route;
207+
}
208+
209+
$expected = [
210+
[
211+
'method' => 'GET',
212+
'route' => '/',
213+
'name' => null,
214+
'handler' => '\\App\\Controllers\\HomeController::index',
215+
],
216+
[
217+
'method' => 'GET',
218+
'route' => 'logout',
219+
'name' => 'logout',
220+
'handler' => '\\App\\Controllers\\AuthController::logout',
221+
],
222+
[
223+
'method' => 'GET',
224+
'route' => 'user/logout',
225+
'name' => null,
226+
'handler' => '\\App\\Controllers\\UserController::logout',
227+
],
228+
[
229+
'method' => 'POST',
230+
'route' => 'login',
231+
'name' => 'login',
232+
'handler' => '\\App\\Controllers\\AuthController::login',
233+
],
234+
[
235+
'method' => 'POST',
236+
'route' => 'user/login',
237+
'name' => null,
238+
'handler' => '\\App\\Controllers\\UserController::login',
239+
],
240+
];
241+
$this->assertSame($expected, $definedRoutes);
242+
}
147243
}

0 commit comments

Comments
 (0)