|
@ -13,16 +13,16 @@ class Router |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
protected $routes = array(); |
|
|
protected $routes = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected $route_name; |
|
|
protected $route_name; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected $default_layout = 'Default'; |
|
|
protected $default_layout = 'Default'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @var Route |
|
|
* @var Route |
|
|
*/ |
|
|
*/ |
|
|
protected $route; |
|
|
protected $route; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function add($name, $route, $action, $params = array(), $layout = null) |
|
|
public function add($name, $route, $action, $params = array(), $layout = null) |
|
|
{ |
|
|
{ |
|
|
if (!$layout) { |
|
|
if (!$layout) { |
|
@ -30,11 +30,11 @@ class Router |
|
|
} |
|
|
} |
|
|
$this->routes[$name] = new Route($route, $action, $params, $layout); |
|
|
$this->routes[$name] = new Route($route, $action, $params, $layout); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function route($request) |
|
|
public function route($request) |
|
|
{ |
|
|
{ |
|
|
$req = explode('/', trim($request, '/')); |
|
|
$req = explode('/', trim($request, '/')); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($this->routes as $name => $route) { |
|
|
foreach ($this->routes as $name => $route) { |
|
|
if ($route->match($req)) { |
|
|
if ($route->match($req)) { |
|
|
$this->route_name = $name; |
|
|
$this->route_name = $name; |
|
@ -45,22 +45,50 @@ class Router |
|
|
} |
|
|
} |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setDefaultLayout($layout = 'Default') |
|
|
public function setDefaultLayout($layout = 'Default') |
|
|
{ |
|
|
{ |
|
|
$this->default_layout = $layout; |
|
|
$this->default_layout = $layout; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getRouteName() |
|
|
public function getRouteName() |
|
|
{ |
|
|
{ |
|
|
return $this->route_name; |
|
|
return $this->route_name; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param null|string $name |
|
|
|
|
|
* @return Route |
|
|
|
|
|
* @throws ErrorException |
|
|
|
|
|
*/ |
|
|
|
|
|
public function getRoute($name = null) |
|
|
|
|
|
{ |
|
|
|
|
|
if (is_null($name)) { |
|
|
|
|
|
return $this->route; |
|
|
|
|
|
} else { |
|
|
|
|
|
if ($this->routeIsExists($name)) { |
|
|
|
|
|
return $this->getRouteByName($name); |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new ErrorException('Unknown route name: "' . $name . '".'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param string $name |
|
|
|
|
|
* @return bool |
|
|
|
|
|
*/ |
|
|
|
|
|
public function routeIsExists($name) |
|
|
|
|
|
{ |
|
|
|
|
|
return array_key_exists($name, $this->routes); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
* @param string $name |
|
|
* @return Route |
|
|
* @return Route |
|
|
*/ |
|
|
*/ |
|
|
public function getRoute() |
|
|
|
|
|
|
|
|
protected function getRouteByName($name) |
|
|
{ |
|
|
{ |
|
|
return $this->route; |
|
|
|
|
|
|
|
|
return $this->routes[$name]; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |