@@ -1151,11 +1151,13 @@ public function environment(string $env, Closure $callback): RouteCollectionInte
11511151 public function reverseRoute (string $ search , ...$ params )
11521152 {
11531153 // Named routes get higher priority.
1154- foreach ($ this ->routesNames as $ collection ) {
1154+ foreach ($ this ->routesNames as $ verb => $ collection ) {
11551155 if (array_key_exists ($ search , $ collection )) {
11561156 $ routeKey = $ collection [$ search ];
11571157
1158- return $ this ->buildReverseRoute ($ routeKey , $ params );
1158+ $ from = $ this ->routes [$ verb ][$ routeKey ]['from ' ];
1159+
1160+ return $ this ->buildReverseRoute ($ from , $ params );
11591161 }
11601162 }
11611163
@@ -1171,8 +1173,9 @@ public function reverseRoute(string $search, ...$params)
11711173 // If it's not a named route, then loop over
11721174 // all routes to find a match.
11731175 foreach ($ this ->routes as $ collection ) {
1174- foreach ($ collection as $ routeKey => $ route ) {
1175- $ to = $ route ['handler ' ];
1176+ foreach ($ collection as $ route ) {
1177+ $ to = $ route ['handler ' ];
1178+ $ from = $ route ['from ' ];
11761179
11771180 // ignore closures
11781181 if (! is_string ($ to )) {
@@ -1196,7 +1199,7 @@ public function reverseRoute(string $search, ...$params)
11961199 continue ;
11971200 }
11981201
1199- return $ this ->buildReverseRoute ($ routeKey , $ params );
1202+ return $ this ->buildReverseRoute ($ from , $ params );
12001203 }
12011204 }
12021205
@@ -1311,21 +1314,21 @@ protected function fillRouteParams(string $from, ?array $params = null): string
13111314 * @param array $params One or more parameters to be passed to the route.
13121315 * The last parameter allows you to set the locale.
13131316 */
1314- protected function buildReverseRoute (string $ routeKey , array $ params ): string
1317+ protected function buildReverseRoute (string $ from , array $ params ): string
13151318 {
13161319 $ locale = null ;
13171320
13181321 // Find all of our back-references in the original route
1319- preg_match_all ('/\(([^)]+)\)/ ' , $ routeKey , $ matches );
1322+ preg_match_all ('/\(([^)]+)\)/ ' , $ from , $ matches );
13201323
13211324 if (empty ($ matches [0 ])) {
1322- if (strpos ($ routeKey , '{locale} ' ) !== false ) {
1325+ if (strpos ($ from , '{locale} ' ) !== false ) {
13231326 $ locale = $ params [0 ] ?? null ;
13241327 }
13251328
1326- $ routeKey = $ this ->replaceLocale ($ routeKey , $ locale );
1329+ $ from = $ this ->replaceLocale ($ from , $ locale );
13271330
1328- return '/ ' . ltrim ($ routeKey , '/ ' );
1331+ return '/ ' . ltrim ($ from , '/ ' );
13291332 }
13301333
13311334 // Locale is passed?
@@ -1336,25 +1339,31 @@ protected function buildReverseRoute(string $routeKey, array $params): string
13361339
13371340 // Build our resulting string, inserting the $params in
13381341 // the appropriate places.
1339- foreach ($ matches [0 ] as $ index => $ pattern ) {
1342+ foreach ($ matches [0 ] as $ index => $ placeholder ) {
13401343 if (! isset ($ params [$ index ])) {
13411344 throw new InvalidArgumentException (
1342- 'Missing argument for " ' . $ pattern . '" in route " ' . $ routeKey . '". '
1345+ 'Missing argument for " ' . $ placeholder . '" in route " ' . $ from . '". '
13431346 );
13441347 }
1348+
1349+ // Remove `(:` and `)` when $placeholder is a placeholder.
1350+ $ placeholderName = substr ($ placeholder , 2 , -1 );
1351+ // or maybe $placeholder is not a placeholder, but a regex.
1352+ $ pattern = $ this ->placeholders [$ placeholderName ] ?? $ placeholder ;
1353+
13451354 if (! preg_match ('#^ ' . $ pattern . '$#u ' , $ params [$ index ])) {
13461355 throw RouterException::forInvalidParameterType ();
13471356 }
13481357
13491358 // Ensure that the param we're inserting matches
13501359 // the expected param type.
1351- $ pos = strpos ($ routeKey , $ pattern );
1352- $ routeKey = substr_replace ($ routeKey , $ params [$ index ], $ pos , strlen ($ pattern ));
1360+ $ pos = strpos ($ from , $ placeholder );
1361+ $ from = substr_replace ($ from , $ params [$ index ], $ pos , strlen ($ placeholder ));
13531362 }
13541363
1355- $ routeKey = $ this ->replaceLocale ($ routeKey , $ locale );
1364+ $ from = $ this ->replaceLocale ($ from , $ locale );
13561365
1357- return '/ ' . ltrim ($ routeKey , '/ ' );
1366+ return '/ ' . ltrim ($ from , '/ ' );
13581367 }
13591368
13601369 /**
0 commit comments