Skip to content

Instantly share code, notes, and snippets.

@zeelot
Created October 13, 2012 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeelot/3886578 to your computer and use it in GitHub Desktop.
Save zeelot/3886578 to your computer and use it in GitHub Desktop.
trying to fix #4113
diff --git a/classes/Kohana/Route.php b/classes/Kohana/Route.php
index fdd2e1b..d8e33fe 100644
--- a/classes/Kohana/Route.php
+++ b/classes/Kohana/Route.php
@@ -531,8 +531,12 @@ class Kohana_Route {
return rtrim($params['host'], '/').'/'.$uri;
}
+ // Keep track of whether an optional param was replaced
+ $provided_optional = FALSE;
+
while (preg_match('#\([^()]++\)#', $uri, $match))
{
+
// Search for the matched value
$search = $match[0];
@@ -545,9 +549,27 @@ class Kohana_Route {
if (isset($params[$param]))
{
+ // Future optional params should be required
+ $provided_optional = TRUE;
+
// Replace the key with the parameter value
$replace = str_replace($key, $params[$param], $replace);
}
+ elseif ($provided_optional)
+ {
+ // Look for a default
+ if (isset($this->_defaults[$param]))
+ {
+ $replace = str_replace($key, $this->_defaults[$param], $replace);
+ }
+ else
+ {
+ // Ungrouped parameters are required
+ throw new Kohana_Exception('Required route parameter not passed: :param', array(
+ ':param' => $param,
+ ));
+ }
+ }
else
{
// This group has missing parameters
@@ -577,7 +599,7 @@ class Kohana_Route {
throw new Kohana_Exception('Required route parameter not passed: :param', array(
':param' => $param,
));
- }
+ }
}
$uri = str_replace($key, $params[$param], $uri);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment