/** * Parse the given URI with the route's pattern to extract the parameters sent by the user * Throws an Exception if the uri does not match the route * * @param Route $route the route matching the uri * @param string $uri the requested URI * * @throws \Exception * * @return array the parameters from the URI */ public function getRouteParams(Route $route, $uri) { if (!$route->matches($uri)) { throw new \Exception("Route doesn't match"); } $params = array(); $routeParts = explode("/", trim($route->getPattern(), "/")); $uriParts = explode("/", trim($uri, "/")); foreach ($routeParts as $index => $part) { if ($part != $uriParts[$index]) { $params[] = $uriParts[$index]; } } return $params; }
/** * Calls the controller linked to the route with the right params * * @param Route $route The route that matched the requested URI * @param array $params an array containing the parameters sent by the user through the URI * * @return void */ public function dispatch(Route $route, array $params) { list($className, $method) = explode(':', $route->getController()); $className .= 'Controller'; $controller = new $className($this->container); return call_user_func_array(array($controller, $method), $params); }