You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.3 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class Router
  12. {
  13. protected $routes = array();
  14. protected $route_name;
  15. protected $default_layout = 'Default';
  16. /**
  17. * @var Route
  18. */
  19. protected $route;
  20. public function add($name, $route, $action, $params = null, $layout = null)
  21. {
  22. if (! $layout) {
  23. $layout = $this->default_layout;
  24. }
  25. $this->routes[$name] = new Route($route, $action, $params, $layout);
  26. }
  27. public function route($request)
  28. {
  29. $req = explode('/', trim($request, '/'));
  30. foreach ($this->routes as $name => $route) {
  31. if ($route->match($req)) {
  32. $this->route_name = $route;
  33. $this->route = $route;
  34. return $this->route;
  35. }
  36. }
  37. return false;
  38. }
  39. public function setDefaultLayout($layout = 'Default')
  40. {
  41. $this->default_layout = $layout;
  42. }
  43. public function getRouteName()
  44. {
  45. return $this->route_name;
  46. }
  47. /**
  48. * @return Route
  49. */
  50. public function getRoute()
  51. {
  52. return $this->route;
  53. }
  54. }