@@ -110,13 +110,16 @@ class RouteCollection implements RouteCollectionInterface
110
110
*
111
111
* [
112
112
* verb => [
113
- * routeName => [
114
- * 'route' => [
115
- * routeKey(regex) => handler,
116
- * or routeKey(regex)(from) => [routeKey(regex)(to) => handler], // redirect
117
- * ],
113
+ * routeKey(regex) => [
114
+ * 'name' => routeName
115
+ * 'handler' => handler,
116
+ * ],
117
+ * // redirect route
118
+ * or routeKey(regex)(from) => [
119
+ * 'name' => routeName
120
+ * 'handler' => [routeKey(regex)(to) => handler],
118
121
* 'redirect' => statusCode,
119
- * ]
122
+ * ],
120
123
* ],
121
124
* ]
122
125
*/
@@ -133,6 +136,30 @@ class RouteCollection implements RouteCollectionInterface
133
136
'cli ' => [],
134
137
];
135
138
139
+ /**
140
+ * Array of routes names
141
+ *
142
+ * @var array
143
+ *
144
+ * [
145
+ * verb => [
146
+ * routeName => routeKey(regex)
147
+ * ],
148
+ * ]
149
+ */
150
+ protected $ routesNames = [
151
+ '* ' => [],
152
+ 'options ' => [],
153
+ 'get ' => [],
154
+ 'head ' => [],
155
+ 'post ' => [],
156
+ 'put ' => [],
157
+ 'delete ' => [],
158
+ 'trace ' => [],
159
+ 'connect ' => [],
160
+ 'cli ' => [],
161
+ ];
162
+
136
163
/**
137
164
* Array of routes options
138
165
*
@@ -504,9 +531,8 @@ public function getRoutes(?string $verb = null): array
504
531
// before any of the generic, "add" routes.
505
532
$ collection = $ this ->routes [$ verb ] + ($ this ->routes ['* ' ] ?? []);
506
533
507
- foreach ($ collection as $ r ) {
508
- $ key = key ($ r ['route ' ]);
509
- $ routes [$ key ] = $ r ['route ' ][$ key ];
534
+ foreach ($ collection as $ routeKey => $ r ) {
535
+ $ routes [$ routeKey ] = $ r ['handler ' ];
510
536
}
511
537
}
512
538
@@ -603,11 +629,16 @@ public function add(string $from, $to, ?array $options = null): RouteCollectionI
603
629
public function addRedirect (string $ from , string $ to , int $ status = 302 )
604
630
{
605
631
// Use the named route's pattern if this is a named route.
606
- if (array_key_exists ($ to , $ this ->routes ['* ' ])) {
607
- $ redirectTo = $ this ->routes ['* ' ][$ to ]['route ' ];
608
- } elseif (array_key_exists ($ to , $ this ->routes ['get ' ])) {
609
- $ redirectTo = $ this ->routes ['get ' ][$ to ]['route ' ];
632
+ if (array_key_exists ($ to , $ this ->routesNames ['* ' ])) {
633
+ $ routeName = $ to ;
634
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
635
+ $ redirectTo = [$ routeKey => $ this ->routes ['* ' ][$ routeKey ]['handler ' ]];
636
+ } elseif (array_key_exists ($ to , $ this ->routesNames ['get ' ])) {
637
+ $ routeName = $ to ;
638
+ $ routeKey = $ this ->routesNames ['get ' ][$ routeName ];
639
+ $ redirectTo = [$ routeKey => $ this ->routes ['get ' ][$ routeKey ]['handler ' ]];
610
640
} else {
641
+ // The named route is not found.
611
642
$ redirectTo = $ to ;
612
643
}
613
644
@@ -623,11 +654,16 @@ public function addRedirect(string $from, string $to, int $status = 302)
623
654
*/
624
655
public function isRedirect (string $ routeKey ): bool
625
656
{
626
- foreach ($ this ->routes ['* ' ] as $ name => $ route ) {
627
- // Named route?
628
- if ($ name === $ routeKey || key ($ route ['route ' ]) === $ routeKey ) {
629
- return isset ($ route ['redirect ' ]) && is_numeric ($ route ['redirect ' ]);
630
- }
657
+ if (isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ])) {
658
+ return true ;
659
+ }
660
+
661
+ // This logic is not used. Should be deprecated?
662
+ $ routeName = $ this ->routes ['* ' ][$ routeKey ]['name ' ] ?? null ;
663
+ if ($ routeName === $ routeKey ) {
664
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
665
+
666
+ return isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ]);
631
667
}
632
668
633
669
return false ;
@@ -640,11 +676,16 @@ public function isRedirect(string $routeKey): bool
640
676
*/
641
677
public function getRedirectCode (string $ routeKey ): int
642
678
{
643
- foreach ($ this ->routes ['* ' ] as $ name => $ route ) {
644
- // Named route?
645
- if ($ name === $ routeKey || key ($ route ['route ' ]) === $ routeKey ) {
646
- return $ route ['redirect ' ] ?? 0 ;
647
- }
679
+ if (isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ])) {
680
+ return $ this ->routes ['* ' ][$ routeKey ]['redirect ' ];
681
+ }
682
+
683
+ // This logic is not used. Should be deprecated?
684
+ $ routeName = $ this ->routes ['* ' ][$ routeKey ]['name ' ] ?? null ;
685
+ if ($ routeName === $ routeKey ) {
686
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
687
+
688
+ return $ this ->routes ['* ' ][$ routeKey ]['redirect ' ];
648
689
}
649
690
650
691
return 0 ;
@@ -1050,7 +1091,7 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
1050
1091
* // Equals 'path/$param1/$param2'
1051
1092
* reverseRoute('Controller::method', $param1, $param2);
1052
1093
*
1053
- * @param string $search Named route or Controller::method
1094
+ * @param string $search Route name or Controller::method
1054
1095
* @param int|string ...$params One or more parameters to be passed to the route.
1055
1096
* The last parameter allows you to set the locale.
1056
1097
*
@@ -1059,9 +1100,11 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
1059
1100
public function reverseRoute (string $ search , ...$ params )
1060
1101
{
1061
1102
// Named routes get higher priority.
1062
- foreach ($ this ->routes as $ collection ) {
1103
+ foreach ($ this ->routesNames as $ collection ) {
1063
1104
if (array_key_exists ($ search , $ collection )) {
1064
- return $ this ->buildReverseRoute (key ($ collection [$ search ]['route ' ]), $ params );
1105
+ $ routeKey = $ collection [$ search ];
1106
+
1107
+ return $ this ->buildReverseRoute ($ routeKey , $ params );
1065
1108
}
1066
1109
}
1067
1110
@@ -1077,9 +1120,8 @@ public function reverseRoute(string $search, ...$params)
1077
1120
// If it's not a named route, then loop over
1078
1121
// all routes to find a match.
1079
1122
foreach ($ this ->routes as $ collection ) {
1080
- foreach ($ collection as $ route ) {
1081
- $ routeKey = key ($ route ['route ' ]);
1082
- $ to = $ route ['route ' ][$ routeKey ];
1123
+ foreach ($ collection as $ routeKey => $ route ) {
1124
+ $ to = $ route ['handler ' ];
1083
1125
1084
1126
// ignore closures
1085
1127
if (! is_string ($ to )) {
@@ -1305,7 +1347,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1305
1347
}
1306
1348
1307
1349
// When redirecting to named route, $to is an array like `['zombies' => '\Zombies::index']`.
1308
- if (is_array ($ to ) && count ($ to) === 2 ) {
1350
+ if (is_array ($ to ) && isset ($ to[ 0 ]) ) {
1309
1351
$ to = $ this ->processArrayCallableSyntax ($ from , $ to );
1310
1352
}
1311
1353
@@ -1386,20 +1428,21 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1386
1428
// routes should always be the "source of truth".
1387
1429
// this works only because discovered routes are added just prior
1388
1430
// to attempting to route the request.
1389
- $ fromExists = dot_array_search ( ' *.route. ' . $ routeKey , $ this ->routes [$ verb ] ?? []) !== null ;
1390
- if ((isset ($ this ->routes [$ verb ][$ name ]) || $ fromExists ) && ! $ overwrite ) {
1431
+ $ fromExists = isset ( $ this ->routes [$ verb ][ $ routeKey ]) ;
1432
+ if ((isset ($ this ->routesNames [$ verb ][$ name ]) || $ fromExists ) && ! $ overwrite ) {
1391
1433
return ;
1392
1434
}
1393
1435
1394
- $ this ->routes [$ verb ][$ name ] = [
1395
- 'route ' => [$ routeKey => $ to ],
1436
+ $ this ->routes [$ verb ][$ routeKey ] = [
1437
+ 'name ' => $ name ,
1438
+ 'handler ' => $ to ,
1396
1439
];
1397
-
1398
1440
$ this ->routesOptions [$ verb ][$ routeKey ] = $ options ;
1441
+ $ this ->routesNames [$ verb ][$ name ] = $ routeKey ;
1399
1442
1400
1443
// Is this a redirect?
1401
1444
if (isset ($ options ['redirect ' ]) && is_numeric ($ options ['redirect ' ])) {
1402
- $ this ->routes ['* ' ][$ name ]['redirect ' ] = $ options ['redirect ' ];
1445
+ $ this ->routes ['* ' ][$ routeKey ]['redirect ' ] = $ options ['redirect ' ];
1403
1446
}
1404
1447
}
1405
1448
@@ -1524,12 +1567,15 @@ private function determineCurrentSubdomain()
1524
1567
*/
1525
1568
public function resetRoutes ()
1526
1569
{
1527
- $ this ->routes = ['* ' => []];
1570
+ $ this ->routes = $ this -> routesNames = ['* ' => []];
1528
1571
1529
1572
foreach ($ this ->defaultHTTPMethods as $ verb ) {
1530
- $ this ->routes [$ verb ] = [];
1573
+ $ this ->routes [$ verb ] = [];
1574
+ $ this ->routesNames [$ verb ] = [];
1531
1575
}
1532
1576
1577
+ $ this ->routesOptions = [];
1578
+
1533
1579
$ this ->prioritizeDetected = false ;
1534
1580
$ this ->didDiscover = false ;
1535
1581
}
@@ -1595,9 +1641,8 @@ public function getRegisteredControllers(?string $verb = '*'): array
1595
1641
1596
1642
if ($ verb === '* ' ) {
1597
1643
foreach ($ this ->defaultHTTPMethods as $ tmpVerb ) {
1598
- foreach ($ this ->routes [$ tmpVerb ] as $ route ) {
1599
- $ routeKey = key ($ route ['route ' ]);
1600
- $ controller = $ this ->getControllerName ($ route ['route ' ][$ routeKey ]);
1644
+ foreach ($ this ->routes [$ tmpVerb ] as $ routeKey => $ route ) {
1645
+ $ controller = $ this ->getControllerName ($ route ['handler ' ]);
1601
1646
if ($ controller !== null ) {
1602
1647
$ controllers [] = $ controller ;
1603
1648
}
0 commit comments