Skip to content

Commit 68c5ad1

Browse files
committed
Fix best route matching (discard not matching HTTP methods)
1 parent 889df5a commit 68c5ad1

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/wcmf/lib/presentation/impl/DefaultRequest.php

+16-14
Original file line numberDiff line numberDiff line change
@@ -334,22 +334,24 @@ protected function getBestRoute($routes) {
334334
// order matching routes by number of parameters
335335
$method = $this->getMethod();
336336
usort($routes, function($a, $b) use ($method) {
337-
$numParamsA = $a['numPathParameters'];
338-
$numParamsB = $b['numPathParameters'];
339-
if ($numParamsA == $numParamsB) {
340-
$numPatternsA = $a['numPathPatterns'];
341-
$numPatternsB = $b['numPathPatterns'];
342-
if ($numPatternsA == $numPatternsB) {
343-
$hasMethodA = in_array($method, $a['methods']);
344-
$hasMethodB = in_array($method, $b['methods']);
345-
return ($hasMethodA && !$hasMethodB) ? -1 :
346-
((!$hasMethodA && $hasMethodB) ? 1 : 0);
337+
$hasMethodA = in_array($method, $a['methods']);
338+
$hasMethodB = in_array($method, $b['methods']);
339+
if ($hasMethodA && $hasMethodB) {
340+
$numParamsA = $a['numPathParameters'];
341+
$numParamsB = $b['numPathParameters'];
342+
if ($numParamsA == $numParamsB) {
343+
$numPatternsA = $a['numPathPatterns'];
344+
$numPatternsB = $b['numPathPatterns'];
345+
if ($numPatternsA == $numPatternsB) {
346+
return 0;
347+
}
348+
// more patterns is more specific
349+
return ($numPatternsA < $numPatternsB) ? 1 : -1;
347350
}
348-
// more patterns is more specific
349-
return ($numPatternsA < $numPatternsB) ? 1 : -1;
351+
// less parameters is more specific
352+
return ($numParamsA > $numParamsB) ? 1 : -1;
350353
}
351-
// less parameters is more specific
352-
return ($numParamsA > $numParamsB) ? 1 : -1;
354+
return ($hasMethodA && !$hasMethodB) ? -1 : 1;
353355
});
354356

355357
if (self::$logger->isDebugEnabled()) {

0 commit comments

Comments
 (0)