@@ -116,13 +116,16 @@ class RouteCollection implements RouteCollectionInterface
116
116
*
117
117
* [
118
118
* verb => [
119
- * routeName => [
120
- * 'route' => [
121
- * routeKey(regex) => handler,
122
- * or routeKey(regex)(from) => [routeKey(regex)(to) => handler], // redirect
123
- * ],
119
+ * routeKey(regex) => [
120
+ * 'name' => routeName
121
+ * 'handler' => handler,
122
+ * ],
123
+ * // redirect route
124
+ * or routeKey(regex)(from) => [
125
+ * 'name' => routeName
126
+ * 'handler' => [routeKey(regex)(to) => handler],
124
127
* 'redirect' => statusCode,
125
- * ]
128
+ * ],
126
129
* ],
127
130
* ]
128
131
*/
@@ -139,6 +142,30 @@ class RouteCollection implements RouteCollectionInterface
139
142
'cli ' => [],
140
143
];
141
144
145
+ /**
146
+ * Array of routes names
147
+ *
148
+ * @var array
149
+ *
150
+ * [
151
+ * verb => [
152
+ * routeName => routeKey(regex)
153
+ * ],
154
+ * ]
155
+ */
156
+ protected $ routesNames = [
157
+ '* ' => [],
158
+ 'options ' => [],
159
+ 'get ' => [],
160
+ 'head ' => [],
161
+ 'post ' => [],
162
+ 'put ' => [],
163
+ 'delete ' => [],
164
+ 'trace ' => [],
165
+ 'connect ' => [],
166
+ 'cli ' => [],
167
+ ];
168
+
142
169
/**
143
170
* Array of routes options
144
171
*
@@ -533,9 +560,8 @@ public function getRoutes(?string $verb = null): array
533
560
// before any of the generic, "add" routes.
534
561
$ collection = $ this ->routes [$ verb ] + ($ this ->routes ['* ' ] ?? []);
535
562
536
- foreach ($ collection as $ r ) {
537
- $ key = key ($ r ['route ' ]);
538
- $ routes [$ key ] = $ r ['route ' ][$ key ];
563
+ foreach ($ collection as $ routeKey => $ r ) {
564
+ $ routes [$ routeKey ] = $ r ['handler ' ];
539
565
}
540
566
}
541
567
@@ -632,11 +658,16 @@ public function add(string $from, $to, ?array $options = null): RouteCollectionI
632
658
public function addRedirect (string $ from , string $ to , int $ status = 302 )
633
659
{
634
660
// Use the named route's pattern if this is a named route.
635
- if (array_key_exists ($ to , $ this ->routes ['* ' ])) {
636
- $ redirectTo = $ this ->routes ['* ' ][$ to ]['route ' ];
637
- } elseif (array_key_exists ($ to , $ this ->routes ['get ' ])) {
638
- $ redirectTo = $ this ->routes ['get ' ][$ to ]['route ' ];
661
+ if (array_key_exists ($ to , $ this ->routesNames ['* ' ])) {
662
+ $ routeName = $ to ;
663
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
664
+ $ redirectTo = [$ routeKey => $ this ->routes ['* ' ][$ routeKey ]['handler ' ]];
665
+ } elseif (array_key_exists ($ to , $ this ->routesNames ['get ' ])) {
666
+ $ routeName = $ to ;
667
+ $ routeKey = $ this ->routesNames ['get ' ][$ routeName ];
668
+ $ redirectTo = [$ routeKey => $ this ->routes ['get ' ][$ routeKey ]['handler ' ]];
639
669
} else {
670
+ // The named route is not found.
640
671
$ redirectTo = $ to ;
641
672
}
642
673
@@ -652,11 +683,16 @@ public function addRedirect(string $from, string $to, int $status = 302)
652
683
*/
653
684
public function isRedirect (string $ routeKey ): bool
654
685
{
655
- foreach ($ this ->routes ['* ' ] as $ name => $ route ) {
656
- // Named route?
657
- if ($ name === $ routeKey || key ($ route ['route ' ]) === $ routeKey ) {
658
- return isset ($ route ['redirect ' ]) && is_numeric ($ route ['redirect ' ]);
659
- }
686
+ if (isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ])) {
687
+ return true ;
688
+ }
689
+
690
+ // This logic is not used. Should be deprecated?
691
+ $ routeName = $ this ->routes ['* ' ][$ routeKey ]['name ' ] ?? null ;
692
+ if ($ routeName === $ routeKey ) {
693
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
694
+
695
+ return isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ]);
660
696
}
661
697
662
698
return false ;
@@ -669,11 +705,16 @@ public function isRedirect(string $routeKey): bool
669
705
*/
670
706
public function getRedirectCode (string $ routeKey ): int
671
707
{
672
- foreach ($ this ->routes ['* ' ] as $ name => $ route ) {
673
- // Named route?
674
- if ($ name === $ routeKey || key ($ route ['route ' ]) === $ routeKey ) {
675
- return $ route ['redirect ' ] ?? 0 ;
676
- }
708
+ if (isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ])) {
709
+ return $ this ->routes ['* ' ][$ routeKey ]['redirect ' ];
710
+ }
711
+
712
+ // This logic is not used. Should be deprecated?
713
+ $ routeName = $ this ->routes ['* ' ][$ routeKey ]['name ' ] ?? null ;
714
+ if ($ routeName === $ routeKey ) {
715
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
716
+
717
+ return $ this ->routes ['* ' ][$ routeKey ]['redirect ' ];
677
718
}
678
719
679
720
return 0 ;
@@ -1088,9 +1129,11 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
1088
1129
public function reverseRoute (string $ search , ...$ params )
1089
1130
{
1090
1131
// Named routes get higher priority.
1091
- foreach ($ this ->routes as $ collection ) {
1132
+ foreach ($ this ->routesNames as $ collection ) {
1092
1133
if (array_key_exists ($ search , $ collection )) {
1093
- return $ this ->buildReverseRoute (key ($ collection [$ search ]['route ' ]), $ params );
1134
+ $ routeKey = $ collection [$ search ];
1135
+
1136
+ return $ this ->buildReverseRoute ($ routeKey , $ params );
1094
1137
}
1095
1138
}
1096
1139
@@ -1106,9 +1149,8 @@ public function reverseRoute(string $search, ...$params)
1106
1149
// If it's not a named route, then loop over
1107
1150
// all routes to find a match.
1108
1151
foreach ($ this ->routes as $ collection ) {
1109
- foreach ($ collection as $ route ) {
1110
- $ routeKey = key ($ route ['route ' ]);
1111
- $ to = $ route ['route ' ][$ routeKey ];
1152
+ foreach ($ collection as $ routeKey => $ route ) {
1153
+ $ to = $ route ['handler ' ];
1112
1154
1113
1155
// ignore closures
1114
1156
if (! is_string ($ to )) {
@@ -1334,7 +1376,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1334
1376
}
1335
1377
1336
1378
// When redirecting to named route, $to is an array like `['zombies' => '\Zombies::index']`.
1337
- if (is_array ($ to ) && count ($ to) === 2 ) {
1379
+ if (is_array ($ to ) && isset ($ to[ 0 ]) ) {
1338
1380
$ to = $ this ->processArrayCallableSyntax ($ from , $ to );
1339
1381
}
1340
1382
@@ -1414,20 +1456,21 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1414
1456
// routes should always be the "source of truth".
1415
1457
// this works only because discovered routes are added just prior
1416
1458
// to attempting to route the request.
1417
- $ fromExists = dot_array_search ( ' *.route. ' . $ routeKey , $ this ->routes [$ verb ] ?? []) !== null ;
1418
- if ((isset ($ this ->routes [$ verb ][$ name ]) || $ fromExists ) && ! $ overwrite ) {
1459
+ $ fromExists = isset ( $ this ->routes [$ verb ][ $ routeKey ]) ;
1460
+ if ((isset ($ this ->routesNames [$ verb ][$ name ]) || $ fromExists ) && ! $ overwrite ) {
1419
1461
return ;
1420
1462
}
1421
1463
1422
- $ this ->routes [$ verb ][$ name ] = [
1423
- 'route ' => [$ routeKey => $ to ],
1464
+ $ this ->routes [$ verb ][$ routeKey ] = [
1465
+ 'name ' => $ name ,
1466
+ 'handler ' => $ to ,
1424
1467
];
1425
-
1426
1468
$ this ->routesOptions [$ verb ][$ routeKey ] = $ options ;
1469
+ $ this ->routesNames [$ verb ][$ name ] = $ routeKey ;
1427
1470
1428
1471
// Is this a redirect?
1429
1472
if (isset ($ options ['redirect ' ]) && is_numeric ($ options ['redirect ' ])) {
1430
- $ this ->routes ['* ' ][$ name ]['redirect ' ] = $ options ['redirect ' ];
1473
+ $ this ->routes ['* ' ][$ routeKey ]['redirect ' ] = $ options ['redirect ' ];
1431
1474
}
1432
1475
}
1433
1476
@@ -1568,12 +1611,15 @@ private function determineCurrentSubdomain()
1568
1611
*/
1569
1612
public function resetRoutes ()
1570
1613
{
1571
- $ this ->routes = ['* ' => []];
1614
+ $ this ->routes = $ this -> routesNames = ['* ' => []];
1572
1615
1573
1616
foreach ($ this ->defaultHTTPMethods as $ verb ) {
1574
- $ this ->routes [$ verb ] = [];
1617
+ $ this ->routes [$ verb ] = [];
1618
+ $ this ->routesNames [$ verb ] = [];
1575
1619
}
1576
1620
1621
+ $ this ->routesOptions = [];
1622
+
1577
1623
$ this ->prioritizeDetected = false ;
1578
1624
$ this ->didDiscover = false ;
1579
1625
}
@@ -1639,9 +1685,8 @@ public function getRegisteredControllers(?string $verb = '*'): array
1639
1685
1640
1686
if ($ verb === '* ' ) {
1641
1687
foreach ($ this ->defaultHTTPMethods as $ tmpVerb ) {
1642
- foreach ($ this ->routes [$ tmpVerb ] as $ route ) {
1643
- $ routeKey = key ($ route ['route ' ]);
1644
- $ controller = $ this ->getControllerName ($ route ['route ' ][$ routeKey ]);
1688
+ foreach ($ this ->routes [$ tmpVerb ] as $ routeKey => $ route ) {
1689
+ $ controller = $ this ->getControllerName ($ route ['handler ' ]);
1645
1690
if ($ controller !== null ) {
1646
1691
$ controllers [] = $ controller ;
1647
1692
}
0 commit comments