diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index 7b5278ae..4fd9d761 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -1,6 +1,7 @@ loadedRoute) { - $getParams = ($getParams === null) ? array() : $getParams; - $params = ($this->loadedRoute->getParameters() == null) ? array() : $this->loadedRoute->getParameters(); - $parameters = ($parameters === null) ? array() : $parameters; - return $this->processUrl($this->loadedRoute, null, array_merge($params, $parameters), array_merge($_GET, $getParams)); - } + $controller = ($controller === null) ? '/' : $controller; + $url = array($controller); - return '/'; + if(is_array($parameters)) { + ArrayUtil::append($url, $parameters); + } + return join('/', $url); } public static function getInstance() { diff --git a/src/Pecee/SimpleRouter/RouterEntry.php b/src/Pecee/SimpleRouter/RouterEntry.php index 78300117..32085aae 100644 --- a/src/Pecee/SimpleRouter/RouterEntry.php +++ b/src/Pecee/SimpleRouter/RouterEntry.php @@ -23,6 +23,7 @@ abstract class RouterEntry { protected $callback; protected $parameters; protected $parametersRegex; + protected $regexMatch; public function __construct() { $this->settings = array(); @@ -158,6 +159,17 @@ public function where(array $options) { return $this; } + /** + * Add regular expression match for url + * + * @param string $regex + * @return self + */ + public function match($regex) { + $this->regexMatch = $regex; + return $this; + } + /** * Get settings that are allowed to be inherited by child routes. * diff --git a/src/Pecee/SimpleRouter/RouterRoute.php b/src/Pecee/SimpleRouter/RouterRoute.php index b1db9b66..162ce030 100644 --- a/src/Pecee/SimpleRouter/RouterRoute.php +++ b/src/Pecee/SimpleRouter/RouterRoute.php @@ -6,6 +6,8 @@ class RouterRoute extends RouterEntry { + const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)}'; + protected $url; protected $requestTypes; @@ -18,13 +20,13 @@ public function __construct($url, $callback) { $this->requestTypes = array(); } - protected function parseParameters($url, $multiple = false) { + protected function parseParameters($url, $multiple = false, $regex = self::PARAMETERS_REGEX_MATCH) { $parameters = array(); if($multiple) { - preg_match_all('/{([A-Za-z\-\_]*?)}/is', $url, $parameters); + preg_match_all('/'.$regex.'/is', $url, $parameters); } else { - preg_match('/{([A-Za-z\-\_]*?)}/is', $url, $parameters); + preg_match('/'.$regex.'/is', $url, $parameters); } if(isset($parameters[1]) && count($parameters[1]) > 0) { @@ -42,43 +44,58 @@ public function matchRoute(Request $request) { $url = parse_url($request->getUri()); $url = $url['path']; - $url = explode('/', trim($url, '/')); - $route = explode('/', trim($this->url, '/')); + $route = $this->url; - // Check if url parameter count matches - if(count($url) === count($route)) { + $routeMatch = preg_replace('/'.self::PARAMETERS_REGEX_MATCH.'/is', '', $route); - $parameters = array(); + // Check if url parameter count matches + if(stripos($url, $routeMatch) === 0) { $matches = true; - // Check if url matches - foreach($route as $i => $path) { - $parameter = $this->parseParameters($path); + if($this->regexMatch) { + $parameters = $this->parseParameters($url, true, $this->regexMatch); - // Check if parameter of path matches, otherwise quit.. - if(is_null($parameter) && strtolower($path) != strtolower($url[$i])) { - $matches = false; - break; + // If regex doesn't match, make sure to return an array + if(!is_array($parameters)) { + $parameters = array(); } - // Save parameter if we have one - if($parameter) { - $parameterValue = $url[$i]; - $regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null); + } else { - if($regex !== null) { - // Use the regular expression rule provided to filter the value - $matches = array(); - preg_match('/'.$regex.'/is', $url[$i], $matches); + $url = explode('/', $url); + $route = explode('/', $route); - if(count($matches)) { - $parameterValue = $matches[0]; - } + $parameters = array(); + + // Check if url matches + foreach ($route as $i => $path) { + $parameter = $this->parseParameters($path, false); + + // Check if parameter of path matches, otherwise quit.. + if (is_null($parameter) && strtolower($path) != strtolower($url[$i])) { + $matches = false; + break; } - // Add parameter value - $parameters[$parameter] = $parameterValue; + // Save parameter if we have one + if ($parameter) { + $parameterValue = $url[$i]; + $regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null); + + if ($regex !== null) { + // Use the regular expression rule provided to filter the value + $matches = array(); + preg_match('/' . $regex . '/is', $url[$i], $matches); + + if (count($matches)) { + $parameterValue = $matches[0]; + } + } + + // Add parameter value + $parameters[$parameter] = $parameterValue; + } } }