@@ -116,12 +116,20 @@ class RouteCollection implements RouteCollectionInterface
116
116
*
117
117
* [
118
118
* verb => [
119
- * routeName => [
120
- * 'route' => [
121
- * routeKey(regex) => handler,
122
- * ],
119
+ * routeKey(regex) => [
120
+ * 'name' => routeName
121
+ * 'handler' => handler,
122
+ * 'from' => from,
123
+ * ],
124
+ * ],
125
+ * // redirect route
126
+ * '*' => [
127
+ * routeKey(regex)(from) => [
128
+ * 'name' => routeName
129
+ * 'handler' => [routeKey(regex)(to) => handler],
130
+ * 'from' => from,
123
131
* 'redirect' => statusCode,
124
- * ]
132
+ * ],
125
133
* ],
126
134
* ]
127
135
*/
@@ -138,6 +146,30 @@ class RouteCollection implements RouteCollectionInterface
138
146
'cli ' => [],
139
147
];
140
148
149
+ /**
150
+ * Array of routes names
151
+ *
152
+ * @var array
153
+ *
154
+ * [
155
+ * verb => [
156
+ * routeName => routeKey(regex)
157
+ * ],
158
+ * ]
159
+ */
160
+ protected $ routesNames = [
161
+ '* ' => [],
162
+ 'options ' => [],
163
+ 'get ' => [],
164
+ 'head ' => [],
165
+ 'post ' => [],
166
+ 'put ' => [],
167
+ 'delete ' => [],
168
+ 'trace ' => [],
169
+ 'connect ' => [],
170
+ 'cli ' => [],
171
+ ];
172
+
141
173
/**
142
174
* Array of routes options
143
175
*
@@ -537,9 +569,8 @@ public function getRoutes(?string $verb = null): array
537
569
// before any of the generic, "add" routes.
538
570
$ collection = $ this ->routes [$ verb ] + ($ this ->routes ['* ' ] ?? []);
539
571
540
- foreach ($ collection as $ r ) {
541
- $ key = key ($ r ['route ' ]);
542
- $ routes [$ key ] = $ r ['route ' ][$ key ];
572
+ foreach ($ collection as $ routeKey => $ r ) {
573
+ $ routes [$ routeKey ] = $ r ['handler ' ];
543
574
}
544
575
}
545
576
@@ -638,42 +669,63 @@ public function add(string $from, $to, ?array $options = null): RouteCollectionI
638
669
public function addRedirect (string $ from , string $ to , int $ status = 302 )
639
670
{
640
671
// Use the named route's pattern if this is a named route.
641
- if (array_key_exists ($ to , $ this ->routes ['* ' ])) {
642
- $ to = $ this ->routes ['* ' ][$ to ]['route ' ];
643
- } elseif (array_key_exists ($ to , $ this ->routes ['get ' ])) {
644
- $ to = $ this ->routes ['get ' ][$ to ]['route ' ];
672
+ if (array_key_exists ($ to , $ this ->routesNames ['* ' ])) {
673
+ $ routeName = $ to ;
674
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
675
+ $ redirectTo = [$ routeKey => $ this ->routes ['* ' ][$ routeKey ]['handler ' ]];
676
+ } elseif (array_key_exists ($ to , $ this ->routesNames ['get ' ])) {
677
+ $ routeName = $ to ;
678
+ $ routeKey = $ this ->routesNames ['get ' ][$ routeName ];
679
+ $ redirectTo = [$ routeKey => $ this ->routes ['get ' ][$ routeKey ]['handler ' ]];
680
+ } else {
681
+ // The named route is not found.
682
+ $ redirectTo = $ to ;
645
683
}
646
684
647
- $ this ->create ('* ' , $ from , $ to , ['redirect ' => $ status ]);
685
+ $ this ->create ('* ' , $ from , $ redirectTo , ['redirect ' => $ status ]);
648
686
649
687
return $ this ;
650
688
}
651
689
652
690
/**
653
691
* Determines if the route is a redirecting route.
692
+ *
693
+ * @param string $routeKey routeKey or route name
654
694
*/
655
- public function isRedirect (string $ from ): bool
695
+ public function isRedirect (string $ routeKey ): bool
656
696
{
657
- foreach ($ this ->routes ['* ' ] as $ name => $ route ) {
658
- // Named route?
659
- if ($ name === $ from || key ($ route ['route ' ]) === $ from ) {
660
- return isset ($ route ['redirect ' ]) && is_numeric ($ route ['redirect ' ]);
661
- }
697
+ if (isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ])) {
698
+ return true ;
699
+ }
700
+
701
+ // This logic is not used. Should be deprecated?
702
+ $ routeName = $ this ->routes ['* ' ][$ routeKey ]['name ' ] ?? null ;
703
+ if ($ routeName === $ routeKey ) {
704
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
705
+
706
+ return isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ]);
662
707
}
663
708
664
709
return false ;
665
710
}
666
711
667
712
/**
668
713
* Grabs the HTTP status code from a redirecting Route.
714
+ *
715
+ * @param string $routeKey routeKey or route name
669
716
*/
670
- public function getRedirectCode (string $ from ): int
717
+ public function getRedirectCode (string $ routeKey ): int
671
718
{
672
- foreach ($ this ->routes ['* ' ] as $ name => $ route ) {
673
- // Named route?
674
- if ($ name === $ from || key ($ route ['route ' ]) === $ from ) {
675
- return $ route ['redirect ' ] ?? 0 ;
676
- }
719
+ if (isset ($ this ->routes ['* ' ][$ routeKey ]['redirect ' ])) {
720
+ return $ this ->routes ['* ' ][$ routeKey ]['redirect ' ];
721
+ }
722
+
723
+ // This logic is not used. Should be deprecated?
724
+ $ routeName = $ this ->routes ['* ' ][$ routeKey ]['name ' ] ?? null ;
725
+ if ($ routeName === $ routeKey ) {
726
+ $ routeKey = $ this ->routesNames ['* ' ][$ routeName ];
727
+
728
+ return $ this ->routes ['* ' ][$ routeKey ]['redirect ' ];
677
729
}
678
730
679
731
return 0 ;
@@ -1088,9 +1140,11 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
1088
1140
public function reverseRoute (string $ search , ...$ params )
1089
1141
{
1090
1142
// Named routes get higher priority.
1091
- foreach ($ this ->routes as $ collection ) {
1143
+ foreach ($ this ->routesNames as $ collection ) {
1092
1144
if (array_key_exists ($ search , $ collection )) {
1093
- return $ this ->buildReverseRoute (key ($ collection [$ search ]['route ' ]), $ params );
1145
+ $ routeKey = $ collection [$ search ];
1146
+
1147
+ return $ this ->buildReverseRoute ($ routeKey , $ params );
1094
1148
}
1095
1149
}
1096
1150
@@ -1106,9 +1160,8 @@ public function reverseRoute(string $search, ...$params)
1106
1160
// If it's not a named route, then loop over
1107
1161
// all routes to find a match.
1108
1162
foreach ($ this ->routes as $ collection ) {
1109
- foreach ($ collection as $ route ) {
1110
- $ from = key ($ route ['route ' ]);
1111
- $ to = $ route ['route ' ][$ from ];
1163
+ foreach ($ collection as $ routeKey => $ route ) {
1164
+ $ to = $ route ['handler ' ];
1112
1165
1113
1166
// ignore closures
1114
1167
if (! is_string ($ to )) {
@@ -1132,7 +1185,7 @@ public function reverseRoute(string $search, ...$params)
1132
1185
continue ;
1133
1186
}
1134
1187
1135
- return $ this ->buildReverseRoute ($ from , $ params );
1188
+ return $ this ->buildReverseRoute ($ routeKey , $ params );
1136
1189
}
1137
1190
}
1138
1191
@@ -1247,21 +1300,21 @@ protected function fillRouteParams(string $from, ?array $params = null): string
1247
1300
* @param array $params One or more parameters to be passed to the route.
1248
1301
* The last parameter allows you to set the locale.
1249
1302
*/
1250
- protected function buildReverseRoute (string $ from , array $ params ): string
1303
+ protected function buildReverseRoute (string $ routeKey , array $ params ): string
1251
1304
{
1252
1305
$ locale = null ;
1253
1306
1254
1307
// Find all of our back-references in the original route
1255
- preg_match_all ('/\(([^)]+)\)/ ' , $ from , $ matches );
1308
+ preg_match_all ('/\(([^)]+)\)/ ' , $ routeKey , $ matches );
1256
1309
1257
1310
if (empty ($ matches [0 ])) {
1258
- if (strpos ($ from , '{locale} ' ) !== false ) {
1311
+ if (strpos ($ routeKey , '{locale} ' ) !== false ) {
1259
1312
$ locale = $ params [0 ] ?? null ;
1260
1313
}
1261
1314
1262
- $ from = $ this ->replaceLocale ($ from , $ locale );
1315
+ $ routeKey = $ this ->replaceLocale ($ routeKey , $ locale );
1263
1316
1264
- return '/ ' . ltrim ($ from , '/ ' );
1317
+ return '/ ' . ltrim ($ routeKey , '/ ' );
1265
1318
}
1266
1319
1267
1320
// Locale is passed?
@@ -1279,13 +1332,13 @@ protected function buildReverseRoute(string $from, array $params): string
1279
1332
1280
1333
// Ensure that the param we're inserting matches
1281
1334
// the expected param type.
1282
- $ pos = strpos ($ from , $ pattern );
1283
- $ from = substr_replace ($ from , $ params [$ index ], $ pos , strlen ($ pattern ));
1335
+ $ pos = strpos ($ routeKey , $ pattern );
1336
+ $ routeKey = substr_replace ($ routeKey , $ params [$ index ], $ pos , strlen ($ pattern ));
1284
1337
}
1285
1338
1286
- $ from = $ this ->replaceLocale ($ from , $ locale );
1339
+ $ routeKey = $ this ->replaceLocale ($ routeKey , $ locale );
1287
1340
1288
- return '/ ' . ltrim ($ from , '/ ' );
1341
+ return '/ ' . ltrim ($ routeKey , '/ ' );
1289
1342
}
1290
1343
1291
1344
/**
@@ -1333,7 +1386,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1333
1386
}
1334
1387
1335
1388
// When redirecting to named route, $to is an array like `['zombies' => '\Zombies::index']`.
1336
- if (is_array ($ to ) && count ($ to) === 2 ) {
1389
+ if (is_array ($ to ) && isset ($ to[ 0 ]) ) {
1337
1390
$ to = $ this ->processArrayCallableSyntax ($ from , $ to );
1338
1391
}
1339
1392
@@ -1385,10 +1438,11 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1385
1438
}
1386
1439
}
1387
1440
1441
+ $ routeKey = $ from ;
1388
1442
// Replace our regex pattern placeholders with the actual thing
1389
1443
// so that the Router doesn't need to know about any of this.
1390
1444
foreach ($ this ->placeholders as $ tag => $ pattern ) {
1391
- $ from = str_ireplace (': ' . $ tag , $ pattern , $ from );
1445
+ $ routeKey = str_ireplace (': ' . $ tag , $ pattern , $ routeKey );
1392
1446
}
1393
1447
1394
1448
// If is redirect, No processing
@@ -1403,7 +1457,7 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1403
1457
$ to = '\\' . ltrim ($ to , '\\' );
1404
1458
}
1405
1459
1406
- $ name = $ options ['as ' ] ?? $ from ;
1460
+ $ name = $ options ['as ' ] ?? $ routeKey ;
1407
1461
1408
1462
helper ('array ' );
1409
1463
@@ -1412,20 +1466,22 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
1412
1466
// routes should always be the "source of truth".
1413
1467
// this works only because discovered routes are added just prior
1414
1468
// to attempting to route the request.
1415
- $ fromExists = dot_array_search ( ' *.route. ' . $ from , $ this ->routes [$ verb ] ?? []) !== null ;
1416
- if ((isset ($ this ->routes [$ verb ][$ name ]) || $ fromExists ) && ! $ overwrite ) {
1469
+ $ routeKeyExists = isset ( $ this ->routes [$ verb ][ $ routeKey ]) ;
1470
+ if ((isset ($ this ->routesNames [$ verb ][$ name ]) || $ routeKeyExists ) && ! $ overwrite ) {
1417
1471
return ;
1418
1472
}
1419
1473
1420
- $ this ->routes [$ verb ][$ name ] = [
1421
- 'route ' => [$ from => $ to ],
1474
+ $ this ->routes [$ verb ][$ routeKey ] = [
1475
+ 'name ' => $ name ,
1476
+ 'handler ' => $ to ,
1477
+ 'from ' => $ from ,
1422
1478
];
1423
-
1424
- $ this ->routesOptions [$ verb ][$ from ] = $ options ;
1479
+ $ this -> routesOptions [ $ verb ][ $ routeKey ] = $ options ;
1480
+ $ this ->routesNames [$ verb ][$ name ] = $ routeKey ;
1425
1481
1426
1482
// Is this a redirect?
1427
1483
if (isset ($ options ['redirect ' ]) && is_numeric ($ options ['redirect ' ])) {
1428
- $ this ->routes ['* ' ][$ name ]['redirect ' ] = $ options ['redirect ' ];
1484
+ $ this ->routes ['* ' ][$ routeKey ]['redirect ' ] = $ options ['redirect ' ];
1429
1485
}
1430
1486
}
1431
1487
@@ -1566,12 +1622,15 @@ private function determineCurrentSubdomain()
1566
1622
*/
1567
1623
public function resetRoutes ()
1568
1624
{
1569
- $ this ->routes = ['* ' => []];
1625
+ $ this ->routes = $ this -> routesNames = ['* ' => []];
1570
1626
1571
1627
foreach ($ this ->defaultHTTPMethods as $ verb ) {
1572
- $ this ->routes [$ verb ] = [];
1628
+ $ this ->routes [$ verb ] = [];
1629
+ $ this ->routesNames [$ verb ] = [];
1573
1630
}
1574
1631
1632
+ $ this ->routesOptions = [];
1633
+
1575
1634
$ this ->prioritizeDetected = false ;
1576
1635
$ this ->didDiscover = false ;
1577
1636
}
@@ -1638,8 +1697,7 @@ public function getRegisteredControllers(?string $verb = '*'): array
1638
1697
if ($ verb === '* ' ) {
1639
1698
foreach ($ this ->defaultHTTPMethods as $ tmpVerb ) {
1640
1699
foreach ($ this ->routes [$ tmpVerb ] as $ route ) {
1641
- $ routeKey = key ($ route ['route ' ]);
1642
- $ controller = $ this ->getControllerName ($ route ['route ' ][$ routeKey ]);
1700
+ $ controller = $ this ->getControllerName ($ route ['handler ' ]);
1643
1701
if ($ controller !== null ) {
1644
1702
$ controllers [] = $ controller ;
1645
1703
}
0 commit comments