@@ -1137,6 +1137,11 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
11371137 $ from = trim ($ from , '/ ' );
11381138 }
11391139
1140+ // When redirecting to named route, $to is an array like `['zombies' => '\Zombies::index']`.
1141+ if (is_array ($ to ) && count ($ to ) === 2 ) {
1142+ $ to = $ this ->processArrayCallableSyntax ($ from , $ to );
1143+ }
1144+
11401145 $ options = array_merge ($ this ->currentOptions ?? [], $ options ?? []);
11411146
11421147 // Route priority detect
@@ -1227,6 +1232,47 @@ protected function create(string $verb, string $from, $to, ?array $options = nul
12271232 }
12281233 }
12291234
1235+ private function processArrayCallableSyntax (string $ from , array $ to ): string
1236+ {
1237+ // [classname, method]
1238+ // eg, [Home::class, 'index']
1239+ if (is_callable ($ to , true , $ callableName )) {
1240+ // If the route has placeholders, add params automatically.
1241+ $ params = $ this ->getMethodParams ($ from );
1242+
1243+ return '\\' . $ callableName . $ params ;
1244+ }
1245+
1246+ // [[classname, method], params]
1247+ // eg, [[Home::class, 'index'], '$1/$2']
1248+ if (
1249+ isset ($ to [0 ], $ to [1 ])
1250+ && is_callable ($ to [0 ], true , $ callableName )
1251+ && is_string ($ to [1 ])
1252+ ) {
1253+ $ to = '\\' . $ callableName . '/ ' . $ to [1 ];
1254+ }
1255+
1256+ return $ to ;
1257+ }
1258+
1259+ /**
1260+ * Returns the method param string like `/$1/$2` for placeholders
1261+ */
1262+ private function getMethodParams (string $ from ): string
1263+ {
1264+ preg_match_all ('/\(.+?\)/ ' , $ from , $ matches );
1265+ $ count = is_countable ($ matches [0 ]) ? count ($ matches [0 ]) : 0 ;
1266+
1267+ $ params = '' ;
1268+
1269+ for ($ i = 1 ; $ i <= $ count ; $ i ++) {
1270+ $ params .= '/$ ' . $ i ;
1271+ }
1272+
1273+ return $ params ;
1274+ }
1275+
12301276 /**
12311277 * Compares the subdomain(s) passed in against the current subdomain
12321278 * on this page request.
0 commit comments