From 73ff07ce7dab1ae0755b471c35de8ea36016c8d4 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Thu, 24 May 2012 17:30:17 +0400 Subject: [PATCH] Add method Router::getUri($route_name = null) - Give me this uri on route_name is null, other another route uri from route_name. --- app/router/Route.php | 5 +++++ app/router/Router.php | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/app/router/Route.php b/app/router/Route.php index 5f60df4..7f42f31 100644 --- a/app/router/Route.php +++ b/app/router/Route.php @@ -54,6 +54,11 @@ class Route return true; } + + public function getUri() + { + return '/' . $this->route; + } public function getAction() { diff --git a/app/router/Router.php b/app/router/Router.php index eb44424..1a3b424 100644 --- a/app/router/Router.php +++ b/app/router/Router.php @@ -13,16 +13,16 @@ class Router { protected $routes = array(); - + protected $route_name; - + protected $default_layout = 'Default'; - + /** * @var Route */ protected $route; - + public function add($name, $route, $action, $params = array(), $layout = null) { if (!$layout) { @@ -30,11 +30,11 @@ class Router } $this->routes[$name] = new Route($route, $action, $params, $layout); } - + public function route($request) { $req = explode('/', trim($request, '/')); - + foreach ($this->routes as $name => $route) { if ($route->match($req)) { $this->route_name = $name; @@ -45,17 +45,17 @@ class Router } return false; } - + public function setDefaultLayout($layout = 'Default') { $this->default_layout = $layout; } - + public function getRouteName() { return $this->route_name; } - + /** * @return Route */ @@ -63,4 +63,30 @@ class Router { return $this->route; } + + public function routeIsExists($name) + { + return array_key_exists($name, $this->routes); + } + + public function getRouteByName($name) + { + return $this->routes[$name]; + } + + static public function getUri($route_name = null) + { + $router = FrontController::getInstance()->getRouter(); + if (is_null($route_name)) { + $route = $router->getRoute(); + } else { + if ($router->routeIsExists($route_name)) { + $route = $router->getRouteByName($route_name); + } else { + $btrace = debug_backtrace(); + throw new ErrorException('Unknown route handler: "' . $route_name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.'); + } + } + return $route->getUri(); + } } \ No newline at end of file