|
|
@ -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(); |
|
|
|
} |
|
|
|
} |