From 257d0650d3544436ae4d92f754edaf4cf57d6bb1 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 7 Dec 2012 08:02:18 +0000 Subject: [PATCH] Added support for optional route parameters --- flight/net/Router.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/flight/net/Router.php b/flight/net/Router.php index 5b3a492..f8af965 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -86,9 +86,12 @@ class Router { $str = '(.*)'; } else if ($str != null && $str{0} == '@') { - if (preg_match('/@(\w+)(\:([^\/]*))?/', $str, $matches)) { - $ids[$matches[1]] = true; - return '(?P<'.$matches[1].'>'.(isset($matches[3]) ? $matches[3] : '[^(\/|\?)]+').')'; + if (preg_match('/@(\w+)(\:([^\/]*))?([\(|\)]+)?/', $str, $matches)) { + $ids[$matches[1]] = null; + return '(?P<'.$matches[1].'>' + .(!empty($matches[3]) ? $matches[3] : '[^(\/|\?)]+') + .')' + .(!empty($matches[4]) ? str_replace(')',')?',$matches[4]) : ''); } } return $str; @@ -99,7 +102,9 @@ class Router { // Attempt to match route and named parameters if (preg_match($regex, $url, $matches)) { if (!empty($ids)) { - $this->params = array_intersect_key($matches, $ids); + foreach ($ids as $k => $v) { + $this->params[$k] = (array_key_exists($k, $matches)) ? $matches[$k] : null; + } } $this->matched = $pattern;