Skip to content

Commit 71cb38d

Browse files
authored
Merge pull request #24 from stein189/v2.0.1
Fixed optional parameter bug
2 parents 0a8dff5 + f43f708 commit 71cb38d

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/RouteResolver.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,27 @@ public function resolve(Router $router, $uri, $method)
3939

4040
// if the requested route matches one of the defined routes
4141
if ($route->getUrl() === $requestedUri || preg_match('~^'.$route->getUrl().'$~', $requestedUri, $matches)) {
42+
$argumentArray = array();
4243
$arguments = $this->getArguments($matches);
43-
$arguments = array_combine($route->getArguments(), $arguments);
44+
45+
// check if there route has arguments
46+
if (is_array($route->getArguments()) && count($route->getArguments()) > 0) {
47+
// loop trough all the arguments
48+
foreach ($route->getArguments() as $key => $argument) {
49+
// if the argument exists set the value
50+
if (isset($arguments[$key])) {
51+
$argumentArray[$argument] = $arguments[$key];
52+
} else {
53+
// setting the value to null should only occure when we use an optional parameter
54+
$argumentArray[$argument] = null;
55+
}
56+
}
57+
}
4458

4559
return [
4660
'code' => Route::STATUS_FOUND,
4761
'handler' => $route->getAction(),
48-
'arguments' => $arguments
62+
'arguments' => $argumentArray
4963
];
5064
}
5165
}

0 commit comments

Comments
 (0)