Commit 86e109b7 authored by Taylor Otwell's avatar Taylor Otwell

simplify route parameter parsing and fix default value bug.

parent 2713ee9b
...@@ -78,38 +78,18 @@ class Route { ...@@ -78,38 +78,18 @@ class Route {
*/ */
protected function parameters($uri, $action, $parameters) protected function parameters($uri, $action, $parameters)
{ {
$wildcards = 0;
$defaults = (array) array_get($action, 'defaults'); $defaults = (array) array_get($action, 'defaults');
// We need to determine how many of the default paramters should be merged
// into the parameter array. First, we will count the number of wildcards
// in the route URI and then merge the defaults.
foreach (array_keys(Router::patterns()) as $wildcard)
{
$wildcards += substr_count($uri, $wildcard);
}
$needed = $wildcards - count($parameters);
// If there are less parameters than wildcards, we will figure out how // If there are less parameters than wildcards, we will figure out how
// many parameters we need to inject from the array of defaults and // many parameters we need to inject from the array of defaults and
// merge them in into the main array for the route. // merge them in into the main array for the route.
if ($needed > 0) if (count($defaults) > count($parameters))
{ {
$defaults = array_slice($defaults, count($defaults) - $needed); $defaults = array_slice($defaults, count($parameters));
$parameters = array_merge($parameters, $defaults); $parameters = array_merge($parameters, $defaults);
} }
// If the final number of parameters doesn't match the count of the
// wildcards, we'll pad parameter array with null to cover any of
// the default values that were forgotten.
if (count($parameters) !== $wildcards)
{
$parameters = array_pad($parameters, $wildcards, null);
}
$this->parameters = $parameters; $this->parameters = $parameters;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment