From 01258d15ef96fc7537fd904f53ffa621ab58ddc1 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Fri, 8 Nov 2013 15:58:39 +0100 Subject: [PATCH] Stripping basePath from route url, only adding it when appropriate. Fixes #38 and #20. Fixes #38 and #20. --- AltoRouter.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/AltoRouter.php b/AltoRouter.php index c677d84..9f698f5 100644 --- a/AltoRouter.php +++ b/AltoRouter.php @@ -25,10 +25,6 @@ public function setBasePath($basePath) { */ public function map($method, $route, $target, $name = null) { - if($route != '*') { - $route = $this->basePath . $route; - } - $this->routes[] = array($method, $route, $target, $name); if($name) { @@ -61,7 +57,9 @@ public function generate($routeName, array $params = array()) { // Replace named parameters $route = $this->namedRoutes[$routeName]; - $url = $route; + + // prepend base path to route url again + $url = $this->basePath . $route; if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { @@ -101,6 +99,9 @@ public function match($requestUrl = null, $requestMethod = null) { $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/'; } + // strip base path from request url + $requestUrl = substr($requestUrl, strlen($this->basePath)); + // Strip query string (?a=b) from Request Url if (($strpos = strpos($requestUrl, '?')) !== false) { $requestUrl = substr($requestUrl, 0, $strpos);