diff --git a/php/includes/Route.php b/php/includes/Route.php index 1575a0e..fcee0f3 100644 --- a/php/includes/Route.php +++ b/php/includes/Route.php @@ -16,7 +16,9 @@ class Route $url = (string) $url; // make sure that the URL is suffixed with a forward slash - if(substr($url,-1) !== '/') $url .= '/'; + if(substr($url,-1) !== '/') { + $url .= '/'; + } $this->url = $url; } @@ -55,10 +57,10 @@ class Route private function substituteFilter($matches) { if (isset($matches[1]) && isset($this->filters[$matches[1]])) { - return $this->filters[$matches[1]]; - } - - return "([\w-]+)"; + return $this->filters[$matches[1]]; + } + + return "([\w-]+)"; } public function getParameters() { diff --git a/php/includes/Router.php b/php/includes/Router.php index 5ed4bfc..6a12ef3 100644 --- a/php/includes/Router.php +++ b/php/includes/Router.php @@ -11,9 +11,7 @@ class Router public function map($routeUrl, $target = '', array $args = array()) { $route = new Route(); - $route->setUrl($this->basePath . $routeUrl); - $route->setTarget($target); if(isset($args['methods'])) { @@ -36,11 +34,16 @@ class Router } public function matchCurrentRequest() { - $requestMethod = (isset($_POST['_method']) && ($_method = strtoupper($_POST['_method'])) && in_array($_method,array('PUT','DELETE'))) ? $_method : $_SERVER['REQUEST_METHOD']; + $requestMethod = $_SERVER['REQUEST_METHOD']; + if(isset($_POST['_method']) && in_array(strtoupper($_POST['_method']),array('PUT','DELETE'))) { + $requestMethod = strtoupper($_POST['_method']); + } + $requestUrl = $_SERVER['REQUEST_URI']; // strip GET variables from URL - if(($pos = strpos($requestUrl, '?')) !== false) { + $pos = strpos($requestUrl, '?'); + if($pos !== false) { $requestUrl = substr($requestUrl, 0, $pos); } @@ -50,17 +53,18 @@ class Router public function match($requestUrl, $requestMethod = 'GET') { foreach($this->routes as $route) { - // compare server request method with route's allowed http methods - if(!in_array($requestMethod, $route->getMethods())) continue; + if(!in_array($requestMethod, $route->getMethods())) { + continue; + } // check if request url matches route regex. if not, return false. - if (!preg_match("@^".$route->getRegex()."*$@i", $requestUrl, $matches)) continue; + if (!preg_match("@^".$route->getRegex()."*$@i", $requestUrl, $matches)) { + continue; + } $params = array(); - if (preg_match_all("/:([\w-]+)/", $route->getUrl(), $argument_keys)) { - // grab array with matches $argument_keys = $argument_keys[1]; @@ -71,31 +75,24 @@ class Router } } } - $route->setParameters($params); - return $route; - } - return false; } public function generate($routeName, array $params = array()) { // Check if route exists - if (!isset($this->namedRoutes[$routeName])) + if (!isset($this->namedRoutes[$routeName])) { throw new Exception("No route with the name $routeName has been found."); - + } $route = $this->namedRoutes[$routeName]; $url = $route->getUrl(); - // replace route url with given parameters if ($params && preg_match_all("/:(\w+)/", $url, $param_keys)) { - // grab array with matches $param_keys = $param_keys[1]; - // loop trough parameter names, store matching value in $params array foreach ($param_keys as $i => $key) { if (isset($params[$key])) { @@ -103,7 +100,6 @@ class Router } } } - return $url; } }